Variables, mut and shadowing in Rust
Learn Rust · Beginner · 10 min · After Install Rust and run your first program
Why can't you change a variable in Rust by default?
1Guess
fn main() { let x = 5; let x = x + 1; { let x = x * 2; println!("Inner x is {x}"); } println!("Outer x is {x}"); }2Read
Immutable by default
A variable made with
letcannot be assigned to a second time. Addmutto allow it:let mut count = 0. The Book gives the reason: the compiler guarantees a value you did not markmutnever changes, so you do not have to track it. A constant usesconst, is always immutable, needs a type, and its name is written in capitals.3Code
mut, const and a typed variable
1 / 2const MAX_COUNT: u32 = 3; fn main() { let mut count = 0; let price: f64 = 9.5; count += 1; count += 1; println!("count is {count} of {MAX_COUNT}"); println!("price is {price}"); }Read the program. What will it print?
4Read
Shadowing is not mut
Shadowing means declaring a new variable with the same name:
let x = x + 1. The old variable is hidden from then on. This differs frommutin one way that matters: a newletmakes a new variable, so it may have a different type. Withmutyou can change the value but never the type.5Code
Shadowing that changes the type
1 / 2fn main() { let spaces = " "; let spaces = spaces.len(); println!("{spaces}"); let whole = 7 / 2; let exact = 7.0 / 2.0; println!("{whole} {exact}"); }Read the program. What will it print?
6Fix
fn main() { let score = 10; println!("Score: {score}"); score = 20; println!("Score: {score}"); }7Say it back
8Mission · 8 min
Write a program that makes a mut counter, adds to it twice and prints it. Then shadow a text variable with its length and print that. Try removing mut and read the error.
cargo new vars cd vars cargo run
Free AI coding tools to practise with
- AgentRouter Free Credits
Free AI coding credits: $25 per daily check-in and $50 per invite
No cardVerified
- Cline Free Models
Try select models free in Cline via rotating promotions with a usage quota
No cardVerified
Common questions
- Why are variables immutable by default in Rust?
- It is a safety choice. When a value is not marked mut, the compiler guarantees it never changes, so you can read the code without checking whether something else modified it. If you do need to change a value, you add mut to the declaration and the intent is visible to every reader.
- What is shadowing in Rust?
- Shadowing is declaring a new variable with the same name as an earlier one, for example let x = x + 1. The new variable hides the old one until its scope ends. Because it is a new variable, it can have a different type, which a mut variable cannot. Shadowing inside a block ends when the block ends.
- What is the difference between const and let in Rust?
- A let variable is immutable unless you add mut, and its type can be inferred. A const is always immutable, cannot use mut, must have a type written out and may only be set to a constant expression. Constants can be declared in any scope, including outside every function, and are named in capitals.
Sources
- https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch03-02-data-types.html (read 2026-12-03)
Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026