Rust from Zero Module 1: Hello, Rust Lesson 1.3 12 min

Variables: why Rust values don’t change (unless you ask)

Store values with let, change them with mut, and learn shadowing: Rust’s clever way to reuse a name.

A variable is a name for a value, so you can use the value again without typing it out. In Rust you make one with let.

main.rs Open in Playground
fn main() {
    let name = "Asha";
    let coins = 3;
    println!("{name} has {coins} coins.");
}
Expected output
Asha has 3 coins.

let name = "Asha"; means: make a variable called name and put "Asha" in it. After that, every time you write name, Rust uses "Asha".

A locked box by default

Here is the surprise that makes Rust different from most languages: a variable cannot change after you create it. Think of it as a box with a lock. You put the value in, and the box locks.

let makes a locked box: the value cannot change. let mut makes an unlocked box: you can put a new value in

Why lock it? Because most values in a program should not change, and when one changes by accident it causes the kind of bug that takes hours to find. Rust makes you say out loud which values are allowed to change. Then anyone reading the code, including future you, knows exactly where to look.

Watch what happens when we try to change a locked variable:

Fix the bug

This program does not compile. Read the error, change the code, and run it until it works.

main.rs
fn main() {
    let coins = 3;
    println!("Coins: {coins}");
    coins = 4;
    println!("Coins: {coins}");
}

What the compiler says

error[E0384]: cannot assign twice to immutable variable `coins`
 --> src/main.rs:4:5
  |
2 |     let coins = 3;
  |         ----- first assignment to `coins`
3 |     println!("Coins: {coins}");
4 |     coins = 4;
  |     ^^^^^^^^^ cannot assign twice to immutable variable
  |
help: consider making this binding mutable
  |
2 |     let mut coins = 3;
  |         +++
Hint

The compiler tells you exactly what to add, and where. A variable that can change is declared with let mut.

Show the fix
fn main() {
    let mut coins = 3;
    println!("Coins: {coins}");
    coins = 4;
    println!("Coins: {coins}");
}

It prints

Coins: 3
Coins: 4

Unlock it with mut

Add mut (short for mutable, meaning changeable) and the box is unlocked. Now you can put a new value in with =.

mut.rs Open in Playground
fn main() {
    let mut score = 0;
    println!("Start: {score}");
    score = score + 10;
    println!("After a coin: {score}");
    score += 5;
    println!("After a bonus: {score}");
}
Expected output
Start: 0
After a coin: 10
After a bonus: 15

score += 5 is a shortcut for score = score + 5. You will see it a lot.

Predict, then check

Predict the output

What will this program print?

fn main() {
    let mut lives = 3;
    println!("Lives: {lives}");
    lives -= 1;
    println!("Lives: {lives}");
}
What will this program print?
Show the real output
Lives: 3
Lives: 2

lives starts at 3. Taking 1 away makes it 2, and the second println! shows the new value. The first line was already printed, so it still shows 3.

Shadowing: a new box with the same name

Rust has one more trick. You can make a new variable with the same name using let again. The new one hides, or shadows, the old one. Step through it:

Shadowing, one line at a time

A box named x holding 5
Step 1 of 3 let x = 5; makes a locked box called x holding 5.
A new box named x holding 6 next to the old greyed-out box
Step 2 of 3 let x = x + 1; reads the old x (5), adds 1, and puts 6 in a brand-new box called x. The old box is hidden.
A third box named x holding 12
Step 3 of 3 let x = x * 2; does it again: it reads 6 and makes a new box holding 12. Every box was locked the whole time.
shadowing.rs Open in Playground
fn main() {
    let x = 5;
    let x = x + 1;
    let x = x * 2;
    println!("x is {x}");
}
Expected output
x is 12

Shadowing is handy when you want to transform a value in steps but keep a simple name, and still keep each step locked.

Types: what kind of value is in the box

Every value has a type. 3 is a whole number, "Asha" is text. Rust usually works out the type for you, but you can write it after a colon:

types.rs Open in Playground
fn main() {
    let age: u32 = 30;
    let height: f64 = 1.68;
    let name: &str = "Asha";
    let is_ready: bool = true;
    println!("{name}, {age}, {height} m, ready: {is_ready}");
}
Expected output
Asha, 30, 1.68 m, ready: true
  • u32 is a whole number that cannot be negative.
  • f64 is a number with a decimal point.
  • &str is a piece of text. (The & has a story; it is the star of Module 4.)
  • bool is true or false.

You do not need to memorise these yet. Module 2 covers them properly.

Quick check

Quick check

  1. What happens when you change a variable made with plain let?
  2. Which line makes a variable you can change later?
  3. After let a = 2; and then let a = a + 3; what is a?

Recap

  • let name = value; makes a variable. It is locked: it cannot change.
  • let mut name = value; makes a variable you can change with = or +=.
  • let with the same name again shadows the old variable with a new one.
  • Types like u32, f64, &str and bool say what kind of value a variable holds.

Next: you know enough to start a real project. In the next lesson you will begin building a text adventure game that grows with every module.

Leave a Reply

Your email address will not be published. Required fields are marked *