---
title: "Variables, mut and shadowing in Rust"
slug: "variables-and-mutability"
description: "Learn why let variables are immutable, add mut to change one, use const and see how shadowing differs from mut. Fix the error for assigning twice."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-rust/variables-and-mutability"
---

# Variables, mut and shadowing in Rust

Why can't you change a variable in Rust by default?

## Predict

```rust
fn main() {
    let x = 5;
    let x = x + 1;
    {
        let x = x * 2;
        println!("Inner x is {x}");
    }
    println!("Outer x is {x}");
}
```

What does this program print?

- Inner x is 6, then Outer x is 6
- Inner x is 12, then Outer x is 12
- Inner x is 12, then Outer x is 6

Answer: Inner x is 12, then Outer x is 6 — The outer x was shadowed to 6; the inner x is 12 only inside its block.

Output:

```
Inner x is 12
Outer x is 6
```

Each let makes a new variable with the same name. When the inner block ends, its x is gone and the outer x of 6 is visible again.

## Immutable by default

A variable made with `let` cannot be assigned to a second time. Add `mut` to allow it: `let mut count = 0`. The Book gives the reason: the compiler guarantees a value you did not mark `mut` never changes, so you do not have to track it. A **constant** uses `const`, is always immutable, needs a type, and its name is written in capitals.

## mut, const and a typed variable

```rust
const 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}");
}
```

Output:

```
count is 2 of 3
price is 9.5
```

`count` is `mut`, so `+= 1` is allowed. `MAX_COUNT` is a `const` with the type `u32`, and `price: f64` states its type with a colon.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=3bd6849b24195f1913dbc16d2e718972

## 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 from `mut` in one way that matters: a new `let` makes a new variable, so it may have a different type. With `mut` you can change the value but never the type.

## Shadowing that changes the type

```rust
fn main() {
    let spaces = "   ";
    let spaces = spaces.len();
    println!("{spaces}");
    let whole = 7 / 2;
    let exact = 7.0 / 2.0;
    println!("{whole} {exact}");
}
```

Output:

```
3
3 3.5
```

`spaces` starts as text and becomes a number, which `mut` would not allow. In the second part, `7 / 2` is integer division and gives 3, while `7.0 / 2.0` gives 3.5.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=00bccd06a5ae1b65f7bb9553b9d11d3f

## Fix it

```rust
fn main() {
    let score = 10;
    println!("Score: {score}");
    score = 20;
    println!("Score: {score}");
}
```

This program does not compile. What is the bug?

- score is immutable, so it cannot be assigned a second time
- println! cannot print a variable that changed
- The number 20 needs a type like 20u32

Answer: score is immutable, so it cannot be assigned a second time — The compiler says it in the message: declare it with let mut to allow the change.

Compiler message:

```
error[E0384]: cannot assign twice to immutable variable `score`
```

Fixed:

```rust
fn main() {
    let mut score = 10;
    println!("Score: {score}");
    score = 20;
    println!("Score: {score}");
}
```

Output:

```
Score: 10
Score: 20
```

## Say it back

In your own words: what is the difference between mut and shadowing?

Our sentence: With `mut` you change the value of one variable and its type stays the same; shadowing makes a new variable with the same name, which can have a different type.

## Mission (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.

```sh
cargo new vars
cd vars
cargo run
```

## 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.
