error[E0384]: cannot assign twice to immutable variable
You assigned a new value to a variable declared without mut. Rust variables are immutable by default: once set, they can’t change.
Quick fix: Declare it with let mut, or create a new variable with the same name using let (shadowing).
Code that triggers E0384
fn main() {
let count = 0;
println!("start: {count}");
count = count + 1;
println!("end: {count}");
}What the compiler says
error[E0384]: cannot assign twice to immutable variable `count`
--> src/main.rs:4:5
|
2 | let count = 0;
| ----- first assignment to `count`
3 | println!("start: {count}");
4 | count = count + 1;
| ^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
2 | let mut count = 0;
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0384`.Why Rust raises E0384
let count = 0; creates an immutable binding, so count = count + 1 on line 4 is rejected. Immutability by default is deliberate: when you read Rust code, any variable without mut is guaranteed to keep its first value, which makes code easier to reason about.
How to fix E0384
1. Declare the variable with mut
If the value really changes over time, such as a counter or a running total, make the binding mutable.
fn main() {
let mut count = 0;
println!("start: {count}");
count += 1;
println!("end: {count}");
}2. Shadow it with a new let
If you compute a new value from the old one once, a second let creates a new variable with the same name. Both stay immutable. Shadowing can also change the type, which mut can’t.
fn main() {
let count = 0;
println!("start: {count}");
let count = count + 1;
println!("end: {count}");
}Related errors
- E0596: cannot borrow as mutable, as it is not declared as mutable
- E0594: cannot assign, as it is not declared as mutable
- E0308: mismatched types
Learn the concept behind it
The Variables and Mutability lesson explains the rules behind E0384 with diagrams and more examples. For the official reference, run rustc --explain E0384 or read the E0384 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.