Rust
E0384 Cannot Assign Fix
The binding was declared without mut, and the code assigns to it a second time - in a straight line, in a loop, in a function argument, or in a for-loop variable. This guide separates four shapes that produce E0384 and matches each one to the repair the diagnostic itself points at: mut where the value keeps changing, a new let where a new value is meant.
Written November 24, 2026 · Tested with Rust 1.99.1 · 17 min read
- The rule underneath: immutable by default, mut to change, let again to replace
- How to read the diagnostic
- The first failure: a plain second assignment
- The second failure: a loop counter without mut
Written November 24, 2026
Tested with Rust 1.99.1
What this guide is
Error E0384 is a second assignment to an immutable binding. The error codes index states the cause in one line: an immutable variable was reassigned. Its fix is to add the keyword mut after let when declaring the variable, and it offers an alternative: initialize a new variable, either under a new name or under the same name by shadowing.
The four forms in this guide each reproduce E0384 with a small, complete program, and the assignment lands in a different place in each one. A plain second assignment in main. A counter incremented inside a while loop. A function argument reassigned inside the function that received it. And a for-loop variable reassigned in the loop body. In each family the failing program is small, the full compiler output is shown, and the fix declares the same binding mutable with mut, in the position that family requires. One further program shows the other repair the index offers: shadowing, where a second let creates a new binding instead of assigning to the old one.
Three neighboring shapes are not E0384 and are not in the families. Assigning to a field through an immutable binding, assigning through an immutable reference, and assigning to a variable captured by a closure are E0594. Assigning to a mut variable while it is borrowed is E0506, and assigning to a constant is E0070. The traps section shows each one with its real code.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: immutable by default, mut to change, let again to replace
By default, variables in Rust are immutable. The Rust Book chapter on variables and mutability puts it plainly: when a variable is immutable, once a value is bound to a name, you can't change that value. The first let is the one assignment an immutable binding gets; every later assignment to that name, whether it is written as x = 6, x += 1, or x = x + 1, is a second assignment and draws E0384.
Adding mut in front of the variable name makes the binding mutable, and the Book adds that mut also conveys intent to future readers of the code by indicating that other parts of the code will be changing this variable's value. That is the repair when the same value is meant to keep changing over time: a counter, an accumulator, a variable a function adjusts as it works. The mut goes where the binding is created - after let for a local variable, in front of the argument name for a parameter, and after for for a loop variable - and each family below shows its own position.
Shadowing is a different operation, not a loophole in the same one. Declaring a new variable with the same name as a previous variable shadows the first: the same Book chapter describes the second variable as what the compiler will see when the name is used, in effect until it is itself shadowed or the scope ends. Because a shadow is a new binding, it needs no mut, and because it is a new binding it can also change the type of the value while reusing the name - something assignment to a mut variable cannot do, since assignment must keep the binding's type. Use shadowing when the intent is a transformed value that then stays fixed: parse, convert, or adjust once, then work with the result. Use mut when the intent is repeated change.
Constants sit outside both options. Like immutable variables, constants are values bound to a name and not allowed to change, but the Book lists the differences: you aren't allowed to use mut with constants - constants aren't just immutable by default, they're always immutable - they are declared with the const keyword instead of let with the type always annotated, and they may be set only to a constant expression, not to a value that could only be computed at runtime. There is no form of a constant that accepts an assignment; a value that must change at runtime belongs in a let binding, mutable if it changes more than once. The traps section shows the code a constant assignment actually draws.
How to read the diagnostic
The header names the binding and, for a plain variable, reads "cannot assign twice to immutable variable". For a function argument the wording changes to "cannot assign to immutable argument", which is the tell that the binding to fix is the parameter, not a local variable. The caret span marks the assignment that failed. In the plain-variable, loop-counter, and for-loop families a label on the declaration line above it - "first assignment to" the name - points back to the let or the for that created the binding without mut; the function-argument family carries no such label, and its help instead rewrites the parameter line itself.
Read the help next: "consider making this binding mutable" is present in every family in this guide, and the help rewrites the declaration line with mut inserted and marked with +++, in exactly the position that family needs. Following that rewrite is the whole fix when continued assignment is the intent. In the borrowed-assignment and constant-assignment traps below that help is absent, because mut is not their missing piece — but absence of the help does not by itself rule out E0384: a ref binding, for example, draws E0384 with no such help line, so read the error code itself, not only the help.
The first failure: a plain second assignment
The binding is declared, its value is printed, and then a second value is assigned to the same name. The name resolves and the type matches; the only failing rule is that the binding was never declared mutable:
fn main() {
let x = 5;
println!("{}", x);
x = 6;
println!("{}", x);
}
$ rustc --edition 2021 fail_plain.rs -o fail_plain
error[E0384]: cannot assign twice to immutable variable `x`
--> fail_plain.rs:4:5
|
2 | let x = 5;
| - first assignment to `x`
3 | println!("{}", x);
4 | x = 6;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
2 | let mut x = 5;
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0384`.
The label "first assignment to x" points at the let, and the help rewrites that line with mut added. Follow it exactly:
fn main() {
let mut x = 5;
println!("{}", x);
x = 6;
println!("{}", x);
}
$ rustc --edition 2021 fix_plain.rs -o fix_plain
$ ./fix_plain
5
6
The output is the pair of values in order: 5, then 6. Nothing else in the program changed - the second assignment was always well-formed; it needed a mutable binding to land on.
Where the second value is a transformation of the first and no further change follows, the other repair fits better. A second let creates a new binding under the same name, so there is no second assignment at all:
fn main() {
let x = 5;
let x = x + 1;
println!("{}", x);
}
$ rustc --edition 2021 fix_shadow.rs -o fix_shadow
$ ./fix_shadow
6
The output is 6, the transformed value. The first binding is shadowed, not changed: code between the two let statements would still see 5, and the new binding is itself immutable, so a third line assigning to x would draw E0384 again. Pick this form when the new value is meant to stay fixed, and the mut form when the value will keep changing.
The second failure: a loop counter without mut
A counter exists to change, so a loop is where this error is met most often. The condition reads count correctly; the increment inside the body is an assignment, and it is the binding's second one:
fn main() {
let count = 0;
while count < 3 {
count += 1;
}
println!("{}", count);
}
$ rustc --edition 2021 fail_loop.rs -o fail_loop
error[E0384]: cannot assign twice to immutable variable `count`
--> fail_loop.rs:4:9
|
2 | let count = 0;
| ----- first assignment to `count`
3 | while count < 3 {
4 | 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`.
The failing line is the compound assignment count += 1 - a += is still an assignment, so it needs the same mut a plain = would. The declaration line carries the "first assignment" label, and the help rewrites it:
fn main() {
let mut count = 0;
while count < 3 {
count += 1;
}
println!("{}", count);
}
$ rustc --edition 2021 fix_loop.rs -o fix_loop
$ ./fix_loop
3
The output is 3: the loop ran until the condition failed, and the final count is what the failing program was trying to print. A for loop over a range avoids this shape when the count itself is all that is needed, because the loop variable is supplied by the loop; a counter kept outside the loop, like this one, is a binding the code owns, and it needs mut.
The third failure: a function argument reassigned
A parameter is a binding too, and it is immutable by default in the same way. This function tries to adjust its argument in place before returning it:
fn bump(x: i32) -> i32 {
x = x + 1;
x
}
fn main() {
println!("{}", bump(41));
}
$ rustc --edition 2021 fail_param.rs -o fail_param
error[E0384]: cannot assign to immutable argument `x`
--> fail_param.rs:2:5
|
2 | x = x + 1;
| ^^^^^^^^^ cannot assign to immutable argument
|
help: consider making this binding mutable
|
1 | fn bump(mut x: i32) -> i32 {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0384`.
The header changes its wording for this shape - "cannot assign to immutable argument" - and the help puts the mut in the parameter list, in front of the argument name. That position is local to the function: it changes nothing for the caller, whose value was copied in:
fn bump(mut x: i32) -> i32 {
x = x + 1;
x
}
fn main() {
println!("{}", bump(41));
}
$ rustc --edition 2021 fix_param.rs -o fix_param
$ ./fix_param
42
The output is 42, the argument 41 adjusted by the function. The alternative repair for this shape is to leave the parameter alone and shadow it as the first statement - let x = x + 1; - which suits a parameter that is adjusted once at entry and then only read. What does not work is declaring a separate mutable copy under a new name and returning the original: the assignment has to land on the binding the rest of the function actually uses.
The fourth failure: a for-loop variable reassigned
The for loop binds its variable once per iteration, and that binding is immutable unless the loop declares it mutable. Reassigning it in the body is a second assignment to that iteration's binding:
fn main() {
for i in 0..3 {
println!("start {}", i);
i = 9;
println!("end {}", i);
}
}
$ rustc --edition 2021 fail_for.rs -o fail_for
error[E0384]: cannot assign twice to immutable variable `i`
--> fail_for.rs:4:9
|
2 | for i in 0..3 {
| - first assignment to `i`
3 | println!("start {}", i);
4 | i = 9;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
2 | for mut i in 0..3 {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0384`.
The "first assignment" label points at the for line itself, because that is where the binding is created, and the help inserts mut there, between for and the variable name:
fn main() {
for mut i in 0..3 {
println!("start {}", i);
i = 9;
println!("end {}", i);
}
}
$ rustc --edition 2021 fix_for.rs -o fix_for
$ ./fix_for
start 0
end 9
start 1
end 9
start 2
end 9
The output is the six lines in order: each iteration starts with the value the range supplied - 0, then 1, then 2 - and ends with 9. Note what the fix does not do: the reassignment does not feed back into the loop. The next iteration rebinds i from the range, so i = 9 changes only the rest of that one iteration. If the intent was to change which values the loop visits, the repair is a different loop - a while with a mutable counter, as in the second family - not mut on the loop variable.
The fixes in reach order
- The header says "cannot assign twice to immutable variable" and the value keeps changing: add
mutat the declaration the "first assignment" label points to. - The header says "cannot assign to immutable argument": add
mutin front of the parameter name, or shadow the parameter with aletas the function's first statement. - The label points at a
forline: writefor mutand the variable name - and check first whether the reassignment was meant to affect later iterations, which it cannot. - The second value is a one-time transformation of the first: shadow it - repeat
letwith the same name - instead of assigning, especially when the value's type changes. - The report is E0594, E0506, or E0070 instead: the binding's own
mutis not the whole story - see the traps.
Traps
- Assigning to a field through an immutable binding is E0594, not E0384. The assignment target is
p.x, notpitself, and the compiler reports that a non-mutable value was assigned a value - the error codes index gives that as the definition of E0594, with a struct field as its own example. The repair is the same word in the same place,let mut p, but the code to read the diagnostic by is E0594:
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 1, y: 2 };
p.x = 5;
println!("{}", p.x);
}
$ rustc --edition 2021 probe_field.rs -o probe_field
error[E0594]: cannot assign to `p.x`, as `p` is not declared as mutable
--> probe_field.rs:8:5
|
8 | p.x = 5;
| ^^^^^^^ cannot assign
|
help: consider changing this to be mutable
|
7 | let mut p = Point { x: 1, y: 2 };
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0594`.
- Assigning through an immutable reference is also E0594. Here the binding
rcould even be mutable and the assignment would still fail, because what blocks it is the&reference itself: a shared reference does not grant writing. The help points at the real repair - borrow mutably,&mut, from a binding declaredmut:
fn main() {
let x = 5;
let r = &x;
*r = 6;
println!("{}", x);
}
$ rustc --edition 2021 probe_deref.rs -o probe_deref
error[E0594]: cannot assign to `*r`, which is behind a `&` reference
--> probe_deref.rs:4:5
|
4 | *r = 6;
| ^^^^^^ `r` is a `&` reference, so it cannot be written to
|
help: consider changing this to be a mutable reference
|
3 | let r = &mut x;
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0594`.
- Assigning to a variable captured by a closure is E0594 as well. The closure body assigns to
x, a binding declared outside it withoutmut, and the report names that outer binding:
fn main() {
let x = 5;
let mut set = || {
x = 6;
};
set();
println!("{}", x);
}
$ rustc --edition 2021 probe_capture.rs -o probe_capture
error[E0594]: cannot assign to `x`, as it is not declared as mutable
--> probe_capture.rs:4:9
|
4 | x = 6;
| ^^^^^ cannot assign
|
help: consider changing this to be mutable
|
2 | let mut x = 5;
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0594`.
let mut x on the captured variable. Read the code before reaching for it, though: a closure that only reads its captures draws no assignment error at all, and a direct assignment to an immutable binding — rather than an assignment through a field, a dereference, or a capture — is E0384, not E0594.
- Assigning to a mutable variable while it is borrowed is E0506 - the opposite situation from every family above. This binding has its mut, and the assignment still fails, because a reference to the old value is still live and will be used afterward. The error codes index states the cause: an attempt was made to assign to a borrowed value, since assigning a new value would invalidate the reference:fn main() {
let mut x = 5;
let r = &x;
x = 6;
println!("{} {}", r, x);
}
$ rustc --edition 2021 probe_borrowed.rs -o probe_borrowed
error[E0506]: cannot assign to `x` because it is borrowed
--> probe_borrowed.rs:4:5
|
3 | let r = &x;
| -- `x` is borrowed here
4 | x = 6;
| ^^^^^ `x` is assigned to here but it was already borrowed
5 | println!("{} {}", r, x);
| - borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0506`.
No help line offers mut here, because mutability is not missing. End the borrow before the assignment - use the reference last above the assignment line, or scope it in a block - and the same assignment compiles.
- Assigning to a constant is E0070, not E0384. A constant is not a place a value can be assigned into: the error codes index defines the code as an assignment operator used on a non-place expression, and lists a constant assignment among its examples. There is no mut form of a constant to switch to:
const LIMIT: i32 = 10;
fn main() {
LIMIT = 20;
println!("{}", LIMIT);
}
$ rustc --edition 2021 probe_const.rs -o probe_const
error[E0070]: invalid left-hand side of assignment
--> probe_const.rs:4:11
|
4 | LIMIT = 20;
| ----- ^
| |
| cannot assign to this expression
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0070`.
A value that changes at runtime has to be a variable: declare it with let, with mut if it changes more than once, and keep const for values fixed at compile time.
- A name that was never declared at all is not in this guide's families: using it is an unresolved name, E0425, because there is no binding for an assignment to land on. E0384 starts only once a binding exists and receives a second assignment.
Checklist
- The header read for its exact wording: "immutable variable" for a local or loop binding, "immutable argument" for a parameter.
- The "first assignment" label followed back to the declaration that created the binding.
mutadded at that declaration - afterlet, in front of a parameter name, or afterfor- when the value is meant to keep changing.- A one-time transformation done with a second
letinstead, and the shadowed binding left immutable afterward. - A reassigned loop variable checked for the feedback mistake: assignment inside a
forbody does not change later iterations. - A report of E0594, E0506, or E0070 repaired by its own rule - mutable binding or mutable borrow for E0594, ending the borrow for E0506, a
letvariable instead of a constant for E0070 - not by addingmutto the failing line. - The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- E0510 Cannot Assign in Match Guard Fix
- E0594 Cannot Assign Fix
- E0161 Cannot Move Unsized Value Fix
- Cannot Transmute Zero-Sized Type E0591 Fix
Sources
- Rust, Error codes index: E0384 - the definition of the error: an immutable variable was reassigned; the fix of adding
mutafterletwhen declaring the variable, and the alternative of initializing a new variable, under a new bound name or by shadowing under the same name. - The Rust Programming Language, Variables and Mutability - variables are immutable by default and an immutable value cannot be changed once bound;
mutin front of a variable name makes it mutable and signals that other parts of the code will change its value; constants are always immutable, take nomut, are declared withconstwith the type annotated, and may be set only to a constant expression; shadowing creates a new variable under the same name, which the compiler sees until it is itself shadowed or the scope ends, and which may change the value's type where assignment to amutvariable may not. - Rust, Error codes index: E0594 - a non-mutable value was assigned a value; the fix of declaring the value as mutable with
mut, shown there with a struct field assignment. - Rust, Error codes index: E0506 - an attempt was made to assign to a borrowed value; while the reference still holds the value, it cannot be assigned a new value, as that would invalidate the reference.
- Rust, Error codes index: E0070 - an assignment operator was used on a non-place expression; the left-hand side of an assignment must be a place expression, and assigning to a constant is among the erroneous examples.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.