Variables and Mutability
In Rust, a variable declared with let is immutable by default — once you give it a value, you can't change it. This is deliberate: most variables in a well-written program never need to change, and the compiler catching an accidental reassignment is far better than discovering it as a bug later.
fn main() {
let x = 5;
println!("x = {}", x);
let mut y = 10;
println!("y = {}", y);
y = 20;
println!("y = {}", y);
}x = 5 y = 10 y = 20
x above has no mut, so it can never be reassigned — trying to would be a compile error. y is declared with let mut, which explicitly opts it into being mutable.
Shadowing
Shadowing lets you declare a new variable with the same name as a previous one, which fully replaces it — this is different from mutation: each let spaces creates an entirely new variable, and can even change its type, which plain mutation never could.
fn main() {
let spaces = " ";
let spaces = spaces.len();
println!("spaces = {}", spaces);
}spaces = 3
Next: basic types — Rust's scalar types, and how far type inference actually goes.
Practice exercise
Declare an immutable variable price: f64 = 9.99. Then declare a mutable variable count: i32 = 1, increase it by 1, and print both price and count together using println!("{} {}", price, count).