Learn Solana-rust - 10 Code Examples & CST Typing Practice Test
Solana Rust is the primary language used to write smart contracts (called programs) on the Solana blockchain. It leverages Rust’s safety and performance features to build high-throughput, low-latency decentralized applications.
View all 10 Solana-rust code examples →
Learn SOLANA-RUST with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Rust + Solana Minimal Counter Contract
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Counter {
pub value: u64
}
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, accounts: &[AccountInfo], _instruction_data: &[u8]) -> ProgramResult {
let mut counter = Counter { value: 0 }
counter.value += 1
msg!("Counter value: {}", counter.value)
Ok(())
}
A minimal Solana program written in Rust that increments a counter stored in an account.
Rust + Solana Increment & Reset
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Counter {
pub value: u64
}
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, _accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
let mut counter = Counter { value: 0 }
match instruction_data[0] {
0 => counter.value += 1
1 => counter.value = 0
_ => msg!("Unknown instruction")
}
msg!("Counter value: {}", counter.value)
Ok(())
}
Solana program in Rust to increment and reset a counter stored in account data.
Rust + Solana Greeting Contract
use solana_program::{entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey, account_info::AccountInfo};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, _accounts: &[AccountInfo], _instruction_data: &[u8]) -> ProgramResult {
msg!("Hello from Solana Rust Program")
Ok(())
}
A simple Solana contract in Rust that logs a greeting message.
Rust + Solana Token Balance Reader
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, accounts: &[AccountInfo], _instruction_data: &[u8]) -> ProgramResult {
let account = &accounts[0]
msg!("Account lamports: {}", account.lamports())
Ok(())
}
Reads a token balance from account data in a Solana Rust program.
Rust + Solana Boolean Toggle
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, _accounts: &[AccountInfo], _instruction_data: &[u8]) -> ProgramResult {
let mut flag = false
flag = !flag
msg!("Flag value: {}", flag)
Ok(())
}
A Solana Rust contract that toggles a boolean value and logs it.
Rust + Solana Simple Key-Value Store
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
use std::collections::HashMap;
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, _accounts: &[AccountInfo], _instruction_data: &[u8]) -> ProgramResult {
let mut store: HashMap<u8, u64> = HashMap::new()
store.insert(1, 100)
msg!("Key 1 value: {}", store.get(&1).unwrap())
Ok(())
}
Implements a very basic key-value store using Solana account data in Rust.
Rust + Solana Increment Event
use borsh::{BorshSerialize, BorshDeserialize};
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Counter { pub value: u64 }
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, _accounts: &[AccountInfo], _instruction_data: &[u8]) -> ProgramResult {
let mut counter = Counter { value: 0 }
counter.value += 1
msg!("Counter incremented to {}", counter.value)
Ok(())
}
Solana Rust program that increments a counter and logs an event message.
Rust + Solana Fixed Supply Token
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
use std::collections::HashMap;
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, _accounts: &[AccountInfo], _instruction_data: &[u8]) -> ProgramResult {
let mut balances: HashMap<&str, u64> = HashMap::new()
balances.insert("owner", 1000)
msg!("Owner balance: {}", balances.get("owner").unwrap())
Ok(())
}
A minimal Solana Rust contract implementing a fixed supply token with balances.
Rust + Solana Greeting With Parameter
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, _accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
let greeting = std::str::from_utf8(instruction_data).unwrap()
msg!("Greeting: {}", greeting)
Ok(())
}
Logs a custom greeting passed as instruction data.
Rust + Solana Conditional Counter
use borsh::{BorshSerialize, BorshDeserialize};
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey};
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Counter { pub value: u64 }
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, _accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
let mut counter = Counter { value: 0 }
if instruction_data[0] == 1 {
counter.value += 1
}
msg!("Counter value: {}", counter.value)
Ok(())
}
Increments a counter only if input flag is 1.
Frequently Asked Questions about Solana-rust
What is Solana-rust?
Solana Rust is the primary language used to write smart contracts (called programs) on the Solana blockchain. It leverages Rust’s safety and performance features to build high-throughput, low-latency decentralized applications.
What are the primary use cases for Solana-rust?
Building high-performance DeFi protocols. NFT minting, marketplaces, and auctions. On-chain gaming logic. Cross-program interactions on Solana. Real-time data feeds and oracles
What are the strengths of Solana-rust?
Memory-safe, high-performance contracts. Efficient state management via accounts. Deterministic execution with Solana runtime. Strong ecosystem support with Anchor. Scalable for high-frequency DeFi or gaming apps
What are the limitations of Solana-rust?
Steeper learning curve due to Rust complexity. Limited tooling compared to Ethereum ecosystem. On-chain state management requires careful account design. Deployment requires understanding Solana runtime. Smaller developer community than Solidity
How can I practice Solana-rust typing speed?
CodeSpeedTest offers 10+ real Solana-rust code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.