Topic
Number Types
Every number in Rust has a type. The type tells Rust two things: how big the number can get, and whether it can go negative.
Quick Reference
| Concept | What it means |
|---|---|
| i32 | A whole number, can be positive or negative |
| u64 | A whole number, always positive, used for lamports |
| u8 | A whole number from 0 to 255 |
| f64 | A decimal number |
| as | Converts a value from one number type to another |
| Overflow | When a number is too big for its type, the program panics |
| Term | What it means | When you use it |
|---|---|---|
| i32 | Signed 32-bit integer, can be positive or negative | Default for whole numbers when sign matters |
| u64 | Unsigned 64-bit integer, always positive and very large | Lamport amounts, token balances, timestamps in Solana |
| u8 | Unsigned 8-bit integer, 0 to 255 only | Small counts, byte values |
| f64 | 64-bit floating point, a decimal number | Default for decimals (rarely used in Solana contracts) |
| as | Casts a value from one number type to another | When two variables have mismatched number types |
| Type inference | Rust guesses the type from the value you assign | When you skip the type annotation, Rust picks i32 or f64 |
| Overflow | A number too big for its type, causes a panic | When you hit it, switch to a larger type |