Rust for Solana Programs

Ownership in one pass, stateless programs with separate accounts, PDAs, rent and how Anchor removes boilerplate.

11 min read·5 quiz questions

Rust in one pass

Rust has no garbage collector. Each value has one owner, and the compiler tracks borrows (&T shared, &mut T exclusive) so memory and data races are caught at compile time. Errors are values: Result<T, E> must be handled, usually with the ? operator.

  • Move semantics: passing a value can transfer ownership.
  • Option<T> replaces null; Result<T, E> replaces exceptions.
  • Fighting the borrow checker early is normal and it pays off.

Programs are stateless; accounts hold state

Unlike an EVM contract, a Solana program stores no state of its own. State lives in separate accounts that the caller must list in the instruction. The program receives program_id, the accounts array and an instruction data byte slice, then deserialises and dispatches.

  • Every account a transaction touches must be declared up front.
  • That is what lets Solana execute non-overlapping transactions in parallel.
  • Accounts are marked writable and/or signer per instruction.

PDAs and rent

A Program Derived Address is an address derived from seeds plus the program id with no private key, so only the program can sign for it — the idiomatic way to own per-user state. Accounts must hold a rent-exempt lamport balance sized to their data, which is reclaimable on close.

Anchor and compute units

Anchor adds account validation macros, serialisation and an IDL (its ABI equivalent) so the client can be generated. Instead of gas, each transaction gets a compute-unit budget; exceeding it fails the transaction, and priority fees only affect ordering.

Key terms

Ownership
Rust rule that every value has exactly one owner, enforced at compile time.
Account
Solana storage unit holding lamports and data, owned by a program.
PDA
Program Derived Address with no private key, signable only by its program.
Rent exemption
Minimum lamport balance an account needs for its data size.
Compute unit
Solana's metering unit; the analogue of EVM gas.
IDL
Anchor's interface description, used like an ABI by clients.

Chapter quiz

5 questions · pass mark 75%
  1. 1. Where does a Solana program keep its state?

  2. 2. Why must transactions declare every account they touch?

  3. 3. A PDA is special because…

  4. 4. In Rust, an unhandled Result…

  5. 5. Exceeding the compute-unit budget causes…

Answer every question to submit. Progress for rust-for-solana is saved in this browser.