Ad space (banner)
Lesson 3 / 20

Variables and mutability (let / mut)

This lesson covers how Rust declares variables and what it means for one to be mutable.

You declare a variable with let, but by default its value cannot be changed afterwards (it is immutable). To allow changes you must write let mut, explicitly declaring the variable mutable.

The sample code declares an immutable let name = "Alice"; and a mutable let mut age = 14;, then changes only age.

A common early stumble is trying to change a variable without mut and hitting a compile error. This immutable-by-default design heads off bugs where a value gets overwritten by accident.

In professional work, the habit of adding mut only to the variables that genuinely need to change leads to code with fewer bugs.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Rust directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)