Borsh Structs
Borsh structs are the workhorse for Solana account bodies. Design them for stable field order, explicit sizing, and forward-compatible versioning.
Search across all documentation pages
Borsh structs are the workhorse for Solana account bodies. Design them for stable field order, explicit sizing, and forward-compatible versioning.
#[derive(BorshSerialize, BorshDeserialize)]
pub struct User { pub authority: Pubkey, pub points: u64 }When to reach for this:
#[derive(BorshSerialize, BorshDeserialize)]
pub struct User {
pub authority: Pubkey,
pub points: u64,
pub bump: u8,
}
impl User {
pub const LEN: usize = 32 + 8 + 1;
}What this demonstrates:
Pubkey 32 + u64 8 + u8 1 = 41 bytes body.
Append fields; migrate old accounts.
// Option<Pubkey>: 1 + 32 bytes| Alternative | Use When | Don't Use When |
|---|---|---|
| bytemuck header + Borsh tail | Hybrid | Pure zero-copy |
| Multiple small accounts | Less realloc | More rent total |
| Account compression | Off-chain | On-chain raw bytes |
32 bytes Borsh.
4 byte len + elements - variable rent.
1 byte.
8 bytes LE.
Supported - sums sizes.
Helps init on host.
10 MB theoretical.
Derives space macro.
Document in upgrades.
Single level preferred for clarity.
Borsh ignores Rust align.
Recommended.
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.
Reviewed by Chris St. John·Last updated Jul 19, 2026