Variables & Mutability
Rust flips the default most languages assume: immutable until proven otherwise. Want to mutate? You ask for it explicitly with mut.
Immutable by Default
let x = 5;// x = 6; // ERROR!Treat that as a feature, not a limitation. When you read let x = 5;, you know x will always be 5 in its scope. No need to trace through code to find where it might change.
Mutable Variables
let mut count = 0;count += 1;count += 1;println!("{}", count); // 2mut doubles as documentation. Reading let mut count tells the next person — possibly future you — that this binding will move.
Shadowing
Rust permits shadowing — re-binding the same name to a fresh variable:
let x = 5;let x = x + 1; // new x, shadows the old onelet x = x * 2; // shadows againprintln!("{}", x); // 12Shadowing isn't mutation:
- Shadowing introduces a brand new variable, possibly with a different type.
- Mutation rewrites the existing variable in place.
let spaces = " "; // &strlet spaces = spaces.len(); // usize - different type! OK with shadowingShadowing lets you keep the same name while morphing the value — types included. The classic use: parsing input with let guess: u32 = guess.trim().parse().unwrap();. Before the line, guess is a String; after, it's a u32. Same name, different type, no mutation.
Constants
Constants are immutable, always typed, and live anywhere from global scope to inside a function body:
const MAX_POINTS: u32 = 100_000;const PI: f64 = 3.14159265358979;The compiler inlines them at every call site, so a const is closer to a #define with type-checking than a runtime variable.
Numeric Types
Rust has explicit integer sizes:
| Type | Size | Range |
|---|---|---|
i8 | 8 bits | -128 to 127 |
i16 | 16 bits | -32,768 to 32,767 |
i32 | 32 bits | ~-2 billion to ~2 billion |
i64 | 64 bits | very large |
u8 | 8 bits | 0 to 255 |
u32 | 32 bits | 0 to ~4 billion |
usize | pointer size | used for indexing |
Default integer literal is i32, default float is f64. Type inference picks those when nothing else nails it down.
A fixed width means a fixed ceiling. A plain rustc main.rs (which is what the runner does) produces a debug build, and debug builds panic on integer overflow. Release builds (rustc -O or cargo build --release) wrap silently unless you switch overflow checks back on. When wrapping or clamping is what you want, say so with the explicit methods, and remember that integer as casts never check: they just keep the low bits.
Those ranges come straight out of the bit patterns. Below is an i8 holding 127, its largest value: a zero sign bit and seven ones. Adding 1 carries all the way up and leaves 1000 0000, so press Clear and click just the leftmost bit. Those eight bits read as -128, which is exactly what wrapping_add(1) returns. Then press Fill: 0xFF is -1 as an i8 and 255 as a u8, and nothing in the bits says which.
Tuples and Arrays
let point: (f64, f64) = (3.0, 4.0);println!("{}, {}", point.0, point.1);
let numbers: [i32; 5] = [1, 2, 3, 4, 5];println!("{}", numbers[0]);// println!("{}", numbers[5]); // ERROR: out of bounds (a runtime index panics instead)Hit an out-of-range index and Rust panics — controlled stop, stack trace, the lot. C would have happily handed you whatever bytes lived past the end. That bounds check is non-negotiable.