Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 6: Variables, Mutability, and Shadowing

Prerequisites

You will understand

  • let vs let mut — immutable by default
  • Shadowing is rebinding, not mutation
  • Why Rust defaults to immutability

Reading time

20 min
+ 15 min exercises
Binding Cards

`let` vs `let mut`

let x = 5 binding promised stability let mut x = 5 reassignment authority is explicit
Scope Diagram

Shadowing Creates New Bindings

x = 5 x = 6 x = 12 exit inner scope → outer binding becomes visible again
Comparison

Shadowing vs Mutation Are Different Mechanisms

Shadowing pipeline Mutation attempt port = "8080" port = 8080 port = ValidPort same concept, refined representation mut port = "8080" port = 8080 type mismatch if binding type must stay `&str`

Step 1 - The Problem