Scalar & Compound Types
Rust’s type system has three families: scalars (integers, floats, bool, char), compounds (tuples, arrays), and the rest. Type inference is good but never silent — every variable has a fixed type at compile time.
A tour of the types
EXAMPLE
fn main() {
// Integers (i8..i128, u8..u128, isize, usize)
let small: i8 = -10;
let big: u64 = 1_000_000_000;
let auto = 42; // defaults to i32
// Floats (f32, f64)
let pi: f64 = 3.14159;
let x = 1.0; // defaults to f64
// Bool + char (char is a Unicode scalar, 4 bytes)
let t: bool = true;
let c: char = '❤';
// Tuples — fixed length, mixed types
let user: (String, u32) = (String::from("Ada"), 36);
let (name, age) = user;
// Arrays — fixed length, single type
let nums: [i32; 4] = [1, 2, 3, 4];
let zeros = [0; 100]; // 100 zeros
println!("{name} {age} {} {nums:?}", c);
}
Why it matters
Use usize for indexing, i32/u32 for most ordinary numbers, and explicit i64 only when you need the range. Rust’s default ints catch overflow in debug builds.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
let n: i32 = -7; let u: u64 = 1_000; let f: f64 = 3.14; let b: bool = true; let t: (i32, &str) = (1, "one");Try it Yourself »
Discussion
Loading…