Learn SOLANA-RUST with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
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.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.