Click a card to flip it · 16 terms
In Rust, values and numbers are the basic data a program works with, specifically numeric values like integers and floating-point numbers.
Creates a new variable.
Every time you store a value.
let age = 25;
Allows a variable to change after it is set.
When your program needs to update a value over time.
let mut count = 0;count += 1;
Cannot change after being set.
This is the default in Rust, every variable is immutable unless you add mut.
mut
let x = 5;// x = 10; ← won't compile
A named place to store a value in your program.
Any time you need to remember a piece of data.
let name = "Alice";let age = 25;
Reusing a variable name with a new let, the old value is replaced.
let
Transforming a value while keeping the same name.
let amount = "1000";let amount: u64 = amount.parse().unwrap();
Tells the compiler to ignore this variable or value, silences unused warnings.
When you have a variable you don't need, or want to skip a value in a pattern.
let _unused = 5;let (plan, _) = (1_000, 50);
Unpacking a tuple or struct into individual named variables in one line.
When you need each value separately from a grouped type.
let (plan, fee) = (1_000_000u64, 50_000u64);
A value that never changes, must have an explicit type and cannot be computed at runtime.
Storing fixed limits or rates that never change.
const MAX_FEE: u64 = 50_000;
A whole number, no decimals.
Counts, balances, indexes.
let count: i32 = 10;let balance: u64 = 5_000_000;
An integer that can be negative or positive (i8, i32, i64).
Temperatures, gains/losses, anything that can go below zero.
let temp: i32 = -5;let gain: i64 = -1_000;
An integer that cannot be negative (u8, u32, u64).
Balances, counts, addresses, anything that can only be zero or positive.
let balance: u64 = 1_000_000;
Unsigned 64-bit integer, a whole number that can't be negative. Holds up to 18 quintillion.
Payment amounts, token balances, timestamps on Solana.
let amount: u64 = 5_000_000;
Unsigned 8-bit integer, holds values from 0 to 255 only.
Bytes and wallet address components on Solana.
let byte: u8 = 255;let addr: [u8; 32] = [0; 32];
A decimal number (f32, f64).
Measurements, rarely used in Solana. Prefer integers (lamports) for money.
let price: f64 = 3.14;
A 64-bit decimal number, high precision but not exact.
Avoid in Solana, use integer lamports instead. Decimals cause rounding errors in money.
let rate: f64 = 0.05;// Use u64 lamports for SOL
Converts one type to another.
When two types need to work together and one must be cast to match the other.
let x: i32 = 5;let y = x as u64;