Skip to main content

How to Make a Variable Mutable in Rust with mut

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Rust variables can't be changed after they're set unless you say so: let mut count = 1; makes count mutable, so count = 2; works. Without mut, the compiler stops with error E0384 and suggests adding it.

Syntax​

Syntax
let mut <name> = <value>;
<name> = <new value>;
PartWhat it does
letDeclares a variable.
mutMakes the variable mutable, so it can be assigned again or changed in place.
<name> = <new value>Assigns a new value. Without mut, this doesn't compile.

The Rust Book, Variables and Mutability: "Although variables are immutable by default, you can make them mutable by adding mut in front of the variable name".

The Example​

The example first assigns a new value to a variable declared without mut, to see the error, then adds mut and runs it again.

Prerequisites​

  • Rust, with Cargo. This guide uses Rust 1.100.1.

Step 1: Create the Project and Write the Code​

Terminal
cargo new demo
cd demo

cargo new demo creates a project with a src/main.rs. Replace its contents:

src/main.rs, whose main declares let count = 1 without mut, prints it, assigns count = 2 and prints it again.

src/main.rs
fn main() {
let count = 1;
println!("{count}");
count = 2;
println!("{count}");
}

Step 2: Run It​

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

For more information about this error, try `rustc --explain E0384`.
error: could not compile `demo` (bin "demo") due to 1 previous error

The program didn't compile. -q keeps Cargo quiet, but compiler errors still show.

Step 3: Add mut​

Change the first line of main to let mut count = 1;, as the compiler suggested:

src/main.rs
fn main() {
let mut count = 1;
println!("{count}");
count = 2;
println!("{count}");
}
Terminal
cargo run -q
Output
1
2

A zsh terminal where cargo run first fails with error E0384 cannot assign twice to immutable variable count and the help consider making this binding mutable, then, after adding mut, prints 1 and 2.

What Happened​

  • Without mut, the second assignment was an error: cannot assign twice to immutable variable count. The compiler marked both the first assignment and the second one.
  • The compiler showed the fix: help: consider making this binding mutable, with +++ under the mut it would add.
  • With mut, the program compiled and printed 1 and then 2.

The same rule covers changing part of a value: assigning to a field, such as validation.validate_nbf = true;, or calling a method that changes the value both need the variable to be let mut.

Common Mistakes​

MistakeWhat happensDo this instead
Adding mut to every variableReaders can't tell which values change, and the compiler warns about mut that isn't neededAdd mut only where the value changes
Expecting mut on one variable to let a function change itA function needs a &mut reference to change the caller's valuePass &mut name to it
Using mut to give a value a new typeA variable keeps its typeDeclare a new variable with let, which shadows the old one

Troubleshooting​

SymptomLikely causeFix
error[E0384]: cannot assign twice to immutable variable `count` The variable was declared without mutAdd mut after let
warning: variable does not need to be mutableA variable is mut but never changesRemove the mut

FAQ​

Why are Rust variables immutable by default?​

The Book calls it "one of many nudges Rust gives you to write your code in a way that takes advantage of the safety and easy concurrency that Rust offers." When a value can't change, every part of the code can rely on it.

Is mut the same as a constant?​

No. A let mut variable can change. A const can never change and must have its type written out.

Conclusion​

Rust variables are immutable unless you write let mut. When the compiler stops with E0384, it already tells you where to put the mut.

Additional Resources​