Skip to content
Theory · Variables and Types · lesson 1 of 12

Variables and mut

What you'll learn

What you'll learn:

  • declare variables with let and let mut and explain how shadowing differs from them;
  • declare constants and choose a type for a value: a whole number, a fractional one, bool or char;
  • predict overflow and the result of an as cast;
  • group values into tuples and arrays and print them with {:?}.
A box apples with a locked glass lid and a tile 3 inside, and next to it stands an open box with a mut sticker

In the first theme, the line let apples = 3; gave the number 3 the name apples. Now let's look at what a variable is and what you can do with it.

Picture a box with a label. The label holds a name, and the box holds a value. let takes a new box, sticks the name on it and puts the value inside. After that, you find the box by its name, look inside and, for example, print what it holds.

The box that let creates has its lid closed. You can look inside, but you cannot put another value in. Let's check:

Rust
fn main() {
    let apples = 3;
    println!("{apples}");
    apples = 5; // ✗
    println!("{apples}");
}
TEXT
error[E0384]: cannot assign twice to immutable variable `apples`

Let's see what happened here:

  1. let apples = 3; creates the variable apples and puts 3 in it.
  2. apples = 5; tries to put 5 into the same variable. The compiler answers with an error: you cannot assign a value twice to an immutable variable.
  3. The program does not build, so not even the first 3 is printed. The rule is checked by the compiler, not by the program while it runs.

A variable whose value cannot change is called immutable. In Rust, every variable declared with let is immutable.

Why changes are off by default

Immutability makes code predictable. When you see let without mut, you know the value in that variable will not change, and there is no need to search for the place where it was overwritten. The compiler watches over this, so an accidental overwrite is found before the program runs.

Interviewers often ask this: why are Rust variables immutable by default? The answer is exactly these two reasons: the code is simpler to read, and the compiler catches overwrite errors.

A mutable variable: let mut

To let a value change, you write the word mut after let. This is a box with an open lid: you can put new values into it as many times as you like. let mut is exactly what the help: hint in the full E0384 message suggests.

Rust
let mut apples = 3;
println!("{apples}"); // 3
apples = 5;
println!("{apples}"); // 5
apples = apples * 2;
println!("{apples}"); // 10

Let's go line by line:

  1. let mut apples = 3; creates a mutable variable with the value 3.
  2. apples = 5; puts 5 into the same box, and println! now prints 5.
  3. apples = apples * 2; first reads the old value 5, multiplies it by 2 and puts the result 10 back into apples.

Here the box analogy falls short. If you take a thing out of a real box, the box is empty. Reading a variable means looking at its value, not taking it: after println!, the same number is still in apples.

Put mut only where the value changes. If you write mut and change nothing, the compiler warns: variable does not need to be mutable.

The type of a variable

Every box has a shape: the type of the value it holds. The type is written after the name, following a colon:

Rust
let apples: i32 = 3;
let pears = 4;
println!("{apples} {pears}"); // 3 4
  1. apples: i32 is a variable of type i32, which holds whole numbers.
  2. pears has no type written. The compiler sees the whole number 4 and picks the same type i32 for pears on its own.

You write the type when you need a different one than the compiler would pick; more often it is inferred from the value. mut lets the value change, but not the type: text does not fit into a box for whole numbers. The code let mut apples = 3; apples = "five"; does not compile: error[E0308]: mismatched types.

Common mistake

The variable was created by assignment, without let, the way it is done in Python:

Rust
fn main() {
    total = 10; // ✗
    println!("{total}");
}
TEXT
error[E0425]: cannot find value `total` in this scope

The compiler looks for a variable named total and does not find it: without let there is no box, and an assignment does not create one. The help: hint in the full message suggests adding let: let total = 10;.

Key points

  • let creates a variable, and by default it is immutable: a second assignment is compile error E0384.
  • let mut lets you put new values into the variable, but its type stays the same.
  • Write the type after a colon or leave it to the compiler: a whole number with no hint gets the type i32.
  • An assignment without let does not create a variable.