Space & Rent
Solana accounts must be rent-exempt. Anchor init uses your space attribute to allocate bytes including the 8-byte discriminator.
Search across all documentation pages
Solana accounts must be rent-exempt. Anchor init uses your space attribute to allocate bytes including the 8-byte discriminator.
#[account]
#[derive(InitSpace)]
pub struct User {
pub authority: Pubkey,
pub points: u64,
}
// space = 8 + User::INIT_SPACE
#[account(init, payer = payer, space = 8 + User::INIT_SPACE)]
pub user: Account<'info, User>,When to reach for this: You add fields to on-chain state or create new account types.
#[account]
#[derive(InitSpace)]
pub struct Config {
pub admin: Pubkey,
#[max_len(64)]
pub name: String,
pub fee_bps: u16,
}
#[account(init, payer = payer, space = 8 + Config::INIT_SPACE)]
pub config: Account<'info, Config>,What this demonstrates:
InitSpace derive calculates size at compile timemax_len on strings/vecs bounds allocationString / Vec: need max_len with InitSpace.Rent-exempt minimum depends on allocated size. Use Rent::get()?.minimum_balance(space) in tests to assert funding.
INIT_SPACE.max_len; size unknown. Fix: Use #[max_len(n)] with InitSpace.realloc.close = recipient.| Alternative | Use When | Don't Use When |
|---|---|---|
| realloc constraint | Growing accounts | Fixed-size known upfront |
| zero_copy | Large fixed structs | Small config accounts |
| Manual space calc | Non-Anchor | InitSpace derive |
0.32.1 for anchor-lang, Anchor CLI, and examples in this section.
Yes. Solana CLI 3.0.10 handles keypairs, airdrops, and solana program inspection.
target/idl/<program>.json in your workspace.
Yes, but every unchecked field needs explicit constraints or handler checks.
Use anchor test with Surfpool 0.12.0 or LiteSVM 0.6.x in CI.
Anchor account discriminator; do not strip it when sizing space.
Yes, or publish on-chain IDL so clients have a canonical source.
Run with logs; Anchor prints constraint name and account index.
Yes. This stack targets Agave validators with Solana CLI 3.0.10.
See sibling articles linked in Related for deeper space rent topics.
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 16, 2026