E0594 Cannot Assign Fix
An assignment names a place the code is only allowed to read - a field reached through &self, a value behind a dereferenced shared reference, a slice element, or a field of a struct reached through a shared reference. This guide separates four shapes that produce E0594 and matches each one to the change that makes the place writable: a mutable borrow where the assignment happens, or interior mutability where the signature cannot change.
Written November 23, 2026 · Tested with Rust 1.99.1 · 21 min read
- The rule underneath: assignment needs a writable place, and a shared reference never provides one
- How to read the diagnostic
- The first failure: a method assigning through &self
- The second failure: assignment through a dereferenced shared reference
Written November 23, 2026
Tested with Rust 1.99.1
What this guide is
Error E0594 is a failed assignment. The error codes index defines it in one line: a non-mutable value was assigned a value. The assignment itself is ordinary - =, +=, a field write, an element write - but the place on its left-hand side is not a place this code may write to. In most real code the place is not writable because it sits behind a shared reference: the code holds a & borrow, and a shared borrow is a read-only view, so anything reached only through it - the value itself, its fields, its elements - is read-only too.
The four forms in this guide each reproduce E0594 with a small, complete program, and each reaches the read-only place by a different route. A method that assigns to a field through a &self receiver. A direct assignment through a dereferenced shared reference. An assignment to the first element of a slice held through a shared slice reference. And an assignment to a struct field through a shared reference parameter. In each family the failing program is small, the full compiler output is shown, and the fix makes the borrow on the path to the place mutable - &mut self, &mut, &mut [i32], &mut Point - with the matching mut on the owning binding where the borrow is created. One family also shows the designed exception: where the receiver must stay &self, a RefCell field makes the same method compile without any signature change.
One neighboring code is not E0594 and is worked only in the traps and the decision summary. Calling a method that needs a mutable borrow through a shared reference is E0596, cannot borrow as mutable - the failure there is a borrow attempted by a method call, not an assignment, and the two codes separate exactly on that difference. E0594 also covers one shape with no reference in it at all: assigning to a field of an owned value whose binding was never declared mut, which the error index uses as its own example and the traps section shows.
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: assignment needs a writable place, and a shared reference never provides one
A reference is a borrow: it refers to a value owned by some other variable, without taking ownership of it. The Rust Book chapter on references and borrowing states the default plainly: just as variables are immutable by default, so are references - the code is not allowed to modify something it has a reference to. A shared reference, written &T, can be copied freely and many can exist at once, and that sharing is exactly why none of them may write: any holder of one shared borrow could otherwise change the value out from under every other holder.
Writing requires the other kind of borrow. A mutable reference, &mut T, is exclusive - while it exists, no other reference to the same value may be used - and in exchange it makes the place behind it writable. The Book's fix for its own borrowing example is the pattern every fix in this guide follows: change the owned value to mut, create the mutable reference with &mut where the value is passed, and update the receiving signature to accept the mutable reference. Three coordinated changes, and the third one is the one the diagnostic's help rewrites.
Assignment follows the place, not the spelling of the statement. *r += 1 writes to the value r points at, data[0] = 99 writes to an element of the slice data refers to, p.x = 10 writes to a field of the struct p refers to, and self.count += 1 writes to a field of the value self refers to. In each case the assignment lands behind the reference, so the reference decides whether the place is writable - and behind a & reference it never is. Putting mut on the binding that holds the reference does not change this: let mut r = &x; makes the binding reassignable, so r could be pointed at a different value, but *r still names a place behind a shared reference. The traps section shows that attempt failing with the same E0594.
Interior mutability is the designed exception. The Rust Reference section on interior mutability defines it directly: a type has interior mutability if its internal state can be changed through a shared reference to it, which goes against the usual requirement that the value pointed to by a shared reference is not mutated. UnsafeCell<T> is the only allowed way to disable that requirement, and the standard library builds safe types on it - the Reference names RefCell<T>, which uses run-time borrow checks to enforce the usual rules around multiple references. The Rust Book chapter on RefCell<T> gives the trade: with ordinary references the borrowing rules are enforced at compile time, and with RefCell<T> they are enforced at runtime - breaking them panics instead of failing to compile - which is what allows a value to mutate itself in its methods while appearing immutable to other code. That is the right tool only where the signature genuinely cannot become &mut; where it can, the mutable borrow is the fix, checked for free at compile time.
How to read the diagnostic
The header names the place and the reason in one line: "cannot assign to" the place, "which is behind a & reference". Read the place first, because it tells you which route the assignment took - self.count is a field through a method receiver, *r is a dereference, data[_] is an element of a slice or array, and p.x is a field through a reference parameter. The label under the caret repeats the reason with the name of the reference: "self is a & reference, so it cannot be written to", and the same sentence appears with r, data, and p in the other families.
Read the help next. In every behind-a-reference family here, the help is "consider changing this to be a mutable reference", and it rewrites the one line where the shared borrow is declared - the receiver, the let binding, or the parameter type - adding mut after the &. That rewrite is necessary but not the whole fix: the call sites that create the borrow must pass &mut, and the owning binding must be declared mut before a mutable borrow of it can be taken at all. The fixed programs show all of the changes together. Where the header instead ends "as p is not declared as mutable", with no reference mentioned, the shape is the owned one from the traps: the fix is mut on the binding itself.
The first failure: a method assigning through &self
The method increment takes &self - a shared borrow of the Counter - and assigns to self.count. The field is reached through that borrow, so the place is read-only even though the method belongs to the type:
struct Counter {
count: i32,
}
impl Counter {
fn increment(&self) {
self.count += 1;
}
}
fn main() {
let c = Counter { count: 0 };
c.increment();
println!("{}", c.count);
}
$ rustc --edition 2021 fail_self.rs -o fail_self
error[E0594]: cannot assign to `self.count`, which is behind a `&` reference
--> fail_self.rs:7:9
|
7 | self.count += 1;
| ^^^^^^^^^^^^^^^ `self` is a `&` reference, so it cannot be written to
|
help: consider changing this to be a mutable reference
|
6 | fn increment(&mut self) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0594`.
The caret spans the whole assignment self.count += 1, and the help rewrites the receiver line, adding mut to &self. Follow it, and add the matching mut to the owning binding in main, because taking &mut self at the call requires a mutable owner:
struct Counter {
count: i32,
}
impl Counter {
fn increment(&mut self) {
self.count += 1;
}
}
fn main() {
let mut c = Counter { count: 0 };
c.increment();
println!("{}", c.count);
}
$ rustc --edition 2021 fix_self.rs -o fix_self
$ ./fix_self
1
The output is the value the failing program was trying to print: 1, the count after one increment. The method now borrows the Counter exclusively for the duration of the call, which is what makes the field a writable place; nothing else in the program changes.
The same method can keep its &self receiver when the field itself carries interior mutability. Wrapping the count in a RefCell moves the borrow check to runtime: the method calls borrow_mut() on the field and assigns through the guard that returns, and neither the receiver nor the binding in main needs mut:
use std::cell::RefCell;
struct Counter {
count: RefCell<i32>,
}
impl Counter {
fn increment(&self) {
*self.count.borrow_mut() += 1;
}
}
fn main() {
let c = Counter { count: RefCell::new(0) };
c.increment();
println!("{}", c.count.borrow());
}
$ rustc --edition 2021 fix_self_refcell.rs -o fix_self_refcell
$ ./fix_self_refcell
1
The output is again 1. This is the designed exception from the rule section, not a workaround around it: RefCell exists for the cases where a value must mutate through a shared reference - a trait method whose receiver is fixed as &self is the standard one. The cost is the runtime check: a second live borrow of the same cell panics where the &mut self version would have failed to compile.
The second failure: assignment through a dereferenced shared reference
Here there is no struct and no method - r is a shared borrow of x, and *r += 1 tries to write to x through it. The dereference is what puts the place behind the reference:
fn main() {
let x = 10;
let r = &x;
*r += 1;
println!("{}", x);
}
$ rustc --edition 2021 fail_deref.rs -o fail_deref
error[E0594]: cannot assign to `*r`, which is behind a `&` reference
--> fail_deref.rs:4:5
|
4 | *r += 1;
| ^^^^^^^ `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`.
The caret spans *r += 1 as a whole, and the help rewrites the borrow itself, from &x to &mut x. Follow it, and declare x as mut, because a mutable borrow can only be taken of a mutable binding:
fn main() {
let mut x = 10;
let r = &mut x;
*r += 1;
println!("{}", x);
}
$ rustc --edition 2021 fix_deref.rs -o fix_deref
$ ./fix_deref
11
The output is the value the failing program was trying to print: 11, the original 10 incremented once through the mutable borrow. Note what did not need mut: the binding r itself. A binding holding a &mut can write through it without being declared mut, because writing through the reference is not reassigning the binding - the traps section shows the mirror image of this, a mut binding holding a & that still cannot write.
The third failure: assignment to a slice element through a shared slice reference
set_first receives data as &[i32], a shared borrow of a slice, and assigns to data[0]. Indexing does not change the route: the element is a place behind the shared reference, exactly as a field or a dereference is:
fn set_first(data: &[i32]) {
data[0] = 99;
}
fn main() {
let v = vec![1, 2, 3];
set_first(&v);
println!("{:?}", v);
}
$ rustc --edition 2021 fail_slice.rs -o fail_slice
error[E0594]: cannot assign to `data[_]`, which is behind a `&` reference
--> fail_slice.rs:2:5
|
2 | data[0] = 99;
| ^^^^^^^^^^^^ `data` is a `&` reference, so it cannot be written to
|
help: consider changing this to be a mutable reference
|
1 | fn set_first(data: &mut [i32]) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0594`.
The header names the place as data[_] - the underscore stands for whichever index the assignment used - and the help rewrites the parameter type to &mut [i32]. Follow it, pass &mut v at the call, and declare the vector mut:
fn set_first(data: &mut [i32]) {
data[0] = 99;
}
fn main() {
let mut v = vec![1, 2, 3];
set_first(&mut v);
println!("{:?}", v);
}
$ rustc --edition 2021 fix_slice.rs -o fix_slice
$ ./fix_slice
[99, 2, 3]
The output is the value the failing program was trying to print: [99, 2, 3], the vector with its first element replaced and the other two untouched. The slice type in the signature is what carries the permission - the same function over &mut Vec<i32> would also work, but &mut [i32] states exactly what the function needs: mutable access to a run of elements, from a vector, an array, or another slice alike.
The fourth failure: assignment to a struct field through a shared reference parameter
move_x receives p as &Point and assigns to p.x. The field access goes through the borrow on every call, so the parameter type - not anything inside the function body - is what makes the place read-only:
struct Point {
x: i32,
y: i32,
}
fn move_x(p: &Point) {
p.x = 10;
}
fn main() {
let p = Point { x: 1, y: 2 };
move_x(&p);
println!("{}, {}", p.x, p.y);
}
$ rustc --edition 2021 fail_field_ref.rs -o fail_field_ref
error[E0594]: cannot assign to `p.x`, which is behind a `&` reference
--> fail_field_ref.rs:7:5
|
7 | p.x = 10;
| ^^^^^^^^ `p` is a `&` reference, so it cannot be written to
|
help: consider changing this to be a mutable reference
|
6 | fn move_x(p: &mut Point) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0594`.
The caret spans p.x = 10, the label names p, and the help rewrites the parameter to &mut Point. Follow it, pass &mut p at the call, and declare the Point binding mut:
struct Point {
x: i32,
y: i32,
}
fn move_x(p: &mut Point) {
p.x = 10;
}
fn main() {
let mut p = Point { x: 1, y: 2 };
move_x(&mut p);
println!("{}, {}", p.x, p.y);
}
$ rustc --edition 2021 fix_field_ref.rs -o fix_field_ref
$ ./fix_field_ref
10, 2
The output is the pair of values the failing program was trying to print: 10, 2 - the x field set by the function, and the y field it never touched. This is the same shape as the first family with the receiver made explicit: &self in a method and &Point as a parameter are the same shared borrow, and &mut is the fix in both.
The fixes in reach order
- The header says the place is behind a
&reference and the help rewrites a method receiver: change&selfto&mut self, and declare the owning bindingmutat the construction site. - The help rewrites a
letbinding's borrow: change&xto&mut x, and declare the borrowed valuemut; the binding holding the reference does not itself needmut. - The help rewrites a parameter type - a slice, a struct, any borrowed type: change
&Tto&mut Tin the signature, pass&mutat every call site, and declare each borrowed valuemut. - The assignment is inside a loop over a collection: the iterator is handing out shared references - switch
iter()toiter_mut(), as the traps section shows. - The receiver or signature cannot change - a trait fixes it as
&selfor&T: wrap the field that must change inRefCell(single-threaded) and assign throughborrow_mut(), accepting a runtime borrow check in place of the compile-time one. - The header names no reference and says the binding is not declared as mutable: the value is owned, not borrowed - declare the binding
mut, as the traps section shows.
Traps
- Assigning to a field of an owned value is the same error code with a different header, and no reference is involved. The error index's own example is this shape - a struct value assigned to after construction - and here
pis a plain ownedPointwhose binding was never declaredmut:
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 1, y: 2 };
p.x = 10;
println!("{}, {}", p.x, p.y);
}
$ rustc --edition 2021 probe_nonmut.rs -o probe_nonmut
error[E0594]: cannot assign to `p.x`, as `p` is not declared as mutable
--> probe_nonmut.rs:8:5
|
8 | p.x = 10;
| ^^^^^^^^ 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`.
p is not declared as mutable" instead of naming a reference, the label under the caret is the bare words "cannot assign", and the help adds mut to the let binding itself. Read the header before reaching for &mut: this shape needs the binding fixed, and there is no borrow to change.
- Putting mut on the binding that holds a shared reference does not make the place behind it writable. This program is the second family with let mut r in place of let r - the binding may now be reassigned, but the assignment still goes through a & borrow:fn main() {
let x = 10;
let mut r = &x;
*r += 1;
println!("{}", x);
}
$ rustc --edition 2021 probe_mut_binding.rs -o probe_mut_binding
warning: variable does not need to be mutable
--> probe_mut_binding.rs:3:9
|
3 | let mut r = &x;
| ----^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default
error[E0594]: cannot assign to `*r`, which is behind a `&` reference
--> probe_mut_binding.rs:4:5
|
4 | *r += 1;
| ^^^^^^^ `r` is a `&` reference, so it cannot be written to
|
help: consider changing this to be a mutable reference
|
3 | let mut r = &mut x;
| +++
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0594`.
Two things come back. The warning at the top says the binding does not need to be mutable - nothing ever reassigns r - and the error below it is the same E0594 as the second family, with the help still pointing at the borrow, rewriting the line to &mut x. Mutability of the binding and mutability of the borrow are separate switches: the first governs reassigning the name, the second governs writing through it, and only the second one is on the path to the assigned place.
- Assigning inside a loop over iter() is the third family's shape in iteration form. iter() yields a shared reference to each element, so *x += 1 is an assignment behind a & reference on every pass:
fn main() {
let v = vec![1, 2, 3];
for x in v.iter() {
*x += 1;
}
println!("{:?}", v);
}
$ rustc --edition 2021 probe_iter.rs -o probe_iter
error[E0594]: cannot assign to `*x`, which is behind a `&` reference
--> probe_iter.rs:4:9
|
3 | for x in v.iter() {
| -------- this iterator yields `&` references
4 | *x += 1;
| ^^^^^^^ `x` is a `&` reference, so it cannot be written to
|
help: use mutable method
|
3 | for x in v.iter_mut() {
| ++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0594`.
The diagnostic adds a note the other families do not have - "this iterator yields & references", pointing at the iter() call - and the help is worded differently from the other families: "use mutable method", rewriting iter() to iter_mut(). Follow it, and declare the vector mut:
fn main() {
let mut v = vec![1, 2, 3];
for x in v.iter_mut() {
*x += 1;
}
println!("{:?}", v);
}
With iter_mut(), each x is a &mut reference to an element, and *x += 1 writes through an exclusive borrow instead of a shared one - the same change of borrow kind as the other fixes, made at the iterator instead of at a signature.
- Mutating through a method call is a different error, not this one. push_str needs to borrow its receiver mutably, and calling it on a String reached through a shared reference fails before any assignment is attempted:
fn change(s: &String) {
s.push_str(", world");
}
fn main() {
let s = String::from("hello");
change(&s);
println!("{}", s);
}
$ rustc --edition 2021 probe_e0596.rs -o probe_e0596
error[E0596]: cannot borrow `*s` as mutable, as it is behind a `&` reference
--> probe_e0596.rs:2:5
|
2 | s.push_str(", world");
| ^ `s` is a `&` reference, so it cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
1 | fn change(s: &mut String) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0596`.
The header is E0596, "cannot borrow *s as mutable, as it is behind a & reference", and the label says the value "cannot be borrowed as mutable" where E0594's label says "cannot be written to". The repair is the same change - the help rewrites the parameter to &mut String - but the code separates the two failures: an assignment to a place behind a shared reference is E0594, and a mutable borrow of a place behind a shared reference, which is what a mutating method call performs, is E0596. The error codes index defines E0596 as trying to mutably borrow a non-mutable variable, and the Book's borrowing chapter works this exact push_str call as its E0596 example.
Decision summary
- The header says "cannot assign to" a place "which is behind a
&reference": the assignment lands behind a shared borrow - make the borrow on the path a&mutborrow, at the receiver, the binding, or the parameter the help rewrites, and declare the borrowed valuemut. - The place in the header is
self.<field>: the method receiver is the borrow -&mut self, or aRefCellfield if the receiver is fixed by a trait. - The place in the header is
data[_]or the diagnostic notes an iterator yielding&references: the elements come through a shared borrow -&mut [T]for a slice parameter,iter_mut()in a loop. - The header ends "as" a binding "is not declared as mutable" and names no reference: the value is owned - declare the binding
mut; no borrow changes. - The binding holding the reference is already
mutand the error persists: binding mutability was never the blocker - the borrow itself must become&mut. - The report is E0596 instead: a mutating method call or an explicit
&mutborrow tried to borrow mutably through a shared reference or from a non-mutable binding - the fix is the same&mutchange, but the failing operation is a borrow, not an assignment.
Checklist
- The header read for which of the two E0594 wordings it carries: behind a
&reference, or a binding not declared as mutable - the fix site follows from which one it is. - Every assignment in the failing code traced to its place: a dereference, a field, or an element - and the borrow on that path identified.
- The shared borrow on the path changed to
&mut- receiver,letborrow, parameter, or iterator - following the line the help rewrites. - Every call site updated to pass
&mut, and every value borrowed mutably checked to be declaredmutat its own binding. - Binding mutability and borrow mutability checked separately:
mutadded to the binding holding a reference only where the binding itself is reassigned. - Where the signature could not change, interior mutability chosen deliberately: the field wrapped in
RefCell, the assignment made throughborrow_mut(), and no second live borrow of the same cell held across it. - The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
Sources
- Rust, Error codes index: E0594 - the definition of the error: a non-mutable value was assigned a value; its example assigns to a field of a struct value whose binding was not declared mutable, and fixes it by declaring the binding
mut. - The Rust Programming Language, References and Borrowing - references are immutable by default and a borrowed value cannot be modified through a shared reference; the fix pattern of declaring the value
mut, creating the borrow with&mut, and accepting&mutin the signature; and the exclusivity of a mutable reference, which cannot coexist with any other reference to the same value. - The Rust Reference, Interior mutability - a type has interior mutability if its internal state can be changed through a shared reference to it;
UnsafeCell<T>is the only allowed way to disable the requirement that a value behind a shared reference is not mutated, andRefCell<T>builds on it with run-time borrow checks. - The Rust Programming Language, RefCell
and the Interior Mutability Pattern - interior mutability allows mutation even where immutable references to the data exist;RefCell<T>enforces the borrowing rules at runtime rather than at compile time, panicking on a violation, and is the route to a value mutating itself in methods that take&self; single-threaded use only. - Rust, Error codes index: E0596 - the neighboring borrow error: the code tried to mutably borrow a non-mutable variable, fixed by making the borrowed value mutable; the distinction from E0594 is borrow versus assignment.
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.