How to Make a Variable Mutable in Rust with mut
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
let mut <name> = <value>;
<name> = <new value>;
| Part | What it does |
|---|---|
let | Declares a variable. |
mut | Makes 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
cargo new demo
cd demo
cargo new demo creates a project with a src/main.rs. Replace its contents:

fn main() {
let count = 1;
println!("{count}");
count = 2;
println!("{count}");
}
Step 2: Run It
cargo run -q
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:
fn main() {
let mut count = 1;
println!("{count}");
count = 2;
println!("{count}");
}
cargo run -q
1
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 themutit would add. - With
mut, the program compiled and printed1and then2.
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
| Mistake | What happens | Do this instead |
|---|---|---|
Adding mut to every variable | Readers can't tell which values change, and the compiler warns about mut that isn't needed | Add mut only where the value changes |
Expecting mut on one variable to let a function change it | A function needs a &mut reference to change the caller's value | Pass &mut name to it |
Using mut to give a value a new type | A variable keeps its type | Declare a new variable with let, which shadows the old one |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error[E0384]: cannot assign twice to immutable variable `count` | The variable was declared without mut | Add mut after let |
warning: variable does not need to be mutable | A variable is mut but never changes | Remove 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
- Variables and Mutability, in The Rust Programming Language.
- E0384, in the Rust compiler's error index.
