Fix Rust E0596 "cannot borrow as mutable" — a Vec Without mut, a Function Taking &Vec, a &self Method Pushing to a Field, and a Shared Reference in a Binding
A value will not change, and the compiler says why in one of two wordings: the binding was never declared mutable, or the value is behind a shared reference that never carried mutable permission. This guide separates the four shapes that produce E0596 — a Vec without mut, a function taking a shared reference, a &self method, and a shared reference held in a binding — and matches each one to declaring mut, taking a mutable reference instead, or using interior mutability where a shared reference is all you have.
Written November 22, 2026 · Tested with Rust 1.99.0 · 18 min read
- The rule underneath: mutation needs mutable permission at the binding and at the reference
- How to read the diagnostic
- The first failure: a Vec declared without mut
- The second failure: a function taking &Vec that tries to push
Written November 22, 2026
Tested with Rust 1.99.0
What this guide is
Error E0596 is a mutable borrow attempted without mutable permission. The error codes index states the cause in one line: this error occurs because you tried to mutably borrow a non-mutable variable. Its example is the smallest form — let y = &mut x; over a binding that was never declared mut — and its fix is to make the binding mutable.
The four forms in this guide each reproduce E0596 with a small, complete program. A Vec declared without mut and then pushed to. A function that takes &Vec<i32> and tries to push inside the function. A method that takes &self and tries to push to one of its own fields. And a binding that holds a shared reference, &v, and tries to push through that reference. In each family the failing program is small, the full compiler output is shown, and the fix adds mutable permission at the exact place the diagnostic names — the binding, the reference type, or the method receiver — rather than sprinkling mut wherever it will parse.
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: mutation needs mutable permission at the binding and at the reference
Variables are immutable by default. 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 cannot change that value, and you make a variable mutable by adding mut in front of its name. A method such as push borrows its receiver mutably, so calling it on an immutable binding is a mutable borrow of a non-mutable variable — the error index's definition of E0596.
References are immutable by default in the same way. The Rust Book chapter on references and borrowing states it directly: just as variables are immutable by default, so are references, and you are not allowed to modify something you have a reference to. Its fix for that shape is the mutable reference: first change the value to be mut, then create a mutable reference with &mut where the function is called, and update the function signature to accept a mutable reference. The Rust Reference on borrow operators states the underlying split: for a shared borrow (&), the place may not be mutated, but it may be read or shared again; a mutable borrow (&mut) is evaluated in a mutable place context and is the borrow that carries permission to mutate.
So mutable permission has two independent places, and E0596 names whichever one is missing. The binding must be declared mut if the value itself is to be borrowed mutably. And the reference you hold must be a &mut reference if you are to mutate through it. Adding mut in the wrong place — to a binding that holds a shared reference, for example — changes only what that binding may be reassigned to; it does not change what the reference points with. The families below are four combinations that each produce E0596, and the traps section shows the misplaced-mut forms failing for exactly that reason.
How to read the diagnostic
Two wordings of the same error code appear in this guide, and the wording tells you which permission is missing. "cannot borrow v as mutable, as it is not declared as mutable" names a binding problem: the value is right there, owned in this scope, but its declaration carries no mut. The help line points at the declaration — "consider changing this to be mutable" — and adding mut there is the whole fix.
"cannot borrow ... as mutable, as it is behind a & reference" names a reference problem: the value sits behind a shared reference, and the label underneath states the reason — the named parameter, self, or binding "is a & reference, so it cannot be borrowed as mutable". The help line in this wording points at the reference type — "consider changing this to be a mutable reference" — and the fix changes & to &mut at the signature or the borrow site, plus mut on the underlying binding where the owner is declared. Read the second clause of the header first; it is the diagnosis, and the first clause is only the code.
The first failure: a Vec declared without mut
This is the binding wording in a minimal form. v owns the Vec, nothing else borrows it, and the only defect is the missing word in the declaration: push needs a mutable borrow of v, and v was not declared as mutable:
fn main() {
let v = vec![1, 2, 3];
v.push(4);
println!("{:?}", v);
}
$ rustc --edition 2021 fail_local.rs -o fail_local
error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable
--> fail_local.rs:3:5
|
3 | v.push(4);
| ^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
2 | let mut v = vec![1, 2, 3];
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0596`.
The underline sits on the single v of the push call with the label "cannot borrow as mutable", and the help rewrites the declaration, not the call. Follow it literally — the call was already right:
fn main() {
let mut v = vec![1, 2, 3];
v.push(4);
println!("{:?}", v);
}
$ rustc --edition 2021 fix_local.rs -o fix_local
$ ./fix_local
[1, 2, 3, 4]
The output is the list with the pushed element appended. When the value is owned in the same scope and no reference intervenes, E0596 is a one-word fix at the declaration, and any larger change — restructuring, cloning, a different collection — is work the diagnostic never asked for.
The second failure: a function taking &Vec that tries to push
Here the owner in main is not the problem at all. add_item receives a shared reference and calls push through it, and a shared reference carries no mutable permission no matter how the caller's binding is declared:
fn add_item(values: &Vec<i32>) {
values.push(4);
}
fn main() {
let v = vec![1, 2, 3];
add_item(&v);
println!("{:?}", v);
}
$ rustc --edition 2021 fail_shared.rs -o fail_shared
error[E0596]: cannot borrow `*values` as mutable, as it is behind a `&` reference
--> fail_shared.rs:2:5
|
2 | values.push(4);
| ^^^^^^ `values` is a `&` reference, so it cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
1 | fn add_item(values: &mut Vec<i32>) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0596`.
The header names *values — the value behind the reference — and the label states the reason: "values is a & reference, so it cannot be borrowed as mutable". The Book's treatment of this exact shape says the fix uses a mutable reference, with three coordinated changes: the caller's value is mut, the call passes &mut, and the signature accepts &mut. All three appear in the fix:
fn add_item(values: &mut Vec<i32>) {
values.push(4);
}
fn main() {
let mut v = vec![1, 2, 3];
add_item(&mut v);
println!("{:?}", v);
}
$ rustc --edition 2021 fix_shared.rs -o fix_shared
$ ./fix_shared
[1, 2, 3, 4]
The output is the caller's list with the element appended, mutated in place through the mutable reference. Note what the signature now advertises: a function taking &mut Vec<i32> announces that it mutates its argument, while the &Vec<i32> version announced read-only access and then broke its own announcement — the compiler held it to what it declared.
The third failure: a &self method that tries to push to a field
A method receiver is a reference, so &self is the shared-reference wording wearing method syntax. record takes &self and pushes to self.entries; the push needs a mutable borrow of the field, and the receiver never carried mutable permission:
struct Log {
entries: Vec<String>,
}
impl Log {
fn record(&self, message: &str) {
self.entries.push(String::from(message));
}
}
fn main() {
let log = Log { entries: Vec::new() };
log.record("started");
println!("{:?}", log.entries);
}
$ rustc --edition 2021 fail_self.rs -o fail_self
error[E0596]: cannot borrow `self.entries` as mutable, as it is behind a `&` reference
--> fail_self.rs:7:9
|
7 | self.entries.push(String::from(message));
| ^^^^^^^^^^^^ `self` is a `&` reference, so it cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
6 | fn record(&mut self, message: &str) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0596`.
The header names the field — "cannot borrow self.entries as mutable" — while the label names the receiver: "self is a & reference, so it cannot be borrowed as mutable". The help points at the receiver, and that is the change: &mut self in the method, and a mut binding for the value the method is called on:
struct Log {
entries: Vec<String>,
}
impl Log {
fn record(&mut self, message: &str) {
self.entries.push(String::from(message));
}
}
fn main() {
let mut log = Log { entries: Vec::new() };
log.record("started");
println!("{:?}", log.entries);
}
$ rustc --edition 2021 fix_self.rs -o fix_self
$ ./fix_self
["started"]
The output is the log holding the recorded message. The receiver change also changes the method's contract for every caller: a &self method can be called through shared references and on immutable bindings, a &mut self method cannot — which is why the compiler makes the method declare, in its receiver, whether it mutates. One nearby shape is a different code, not this one: assigning to a field, as in self.count += 1 inside a &self method, is reported as E0594, "cannot assign to self.count, which is behind a & reference", not E0596 — the repair, &mut self, is the same, reached through the assignment diagnostic rather than the borrow one.
The fourth failure: a shared reference held in a binding
This family separates the two places mut can go, because both are visible in three lines. r holds &v — a shared reference. Pushing through r fails not because r's binding lacks mut, and not only because of how v was declared, but because the reference r holds is a & reference:
fn main() {
let v = vec![1, 2, 3];
let r = &v;
r.push(4);
println!("{:?}", v);
}
$ rustc --edition 2021 fail_ref_binding.rs -o fail_ref_binding
error[E0596]: cannot borrow `*r` as mutable, as it is behind a `&` reference
--> fail_ref_binding.rs:4:5
|
4 | r.push(4);
| ^ `r` is a `&` reference, so it cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
3 | let r = &mut v;
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0596`.
The label is the diagnosis again: "r is a & reference, so it cannot be borrowed as mutable", and the help rewrites the borrow, from &v to &mut v. That rewrite has a precondition the help does not state: a mutable borrow of v requires v itself to be declared mut (the traps section shows the borrow-only change failing with the not-declared-as-mutable wording). The complete fix therefore changes both lines — mut on the owner's declaration, and &mut at the borrow:
fn main() {
let mut v = vec![1, 2, 3];
let r = &mut v;
r.push(4);
println!("{:?}", r);
}
$ rustc --edition 2021 fix_ref_binding.rs -o fix_ref_binding
$ ./fix_ref_binding
[1, 2, 3, 4]
The output is the list with the element appended, printed through the mutable reference that did the pushing. Keep the two muts distinct in your mind: let mut v governs the owned value — whether it may be borrowed mutably at all. &mut v is the borrow itself — the permission this particular reference carries. And a third placement, let mut r = &v, would govern only rebinding — whether r may later be pointed at something else — and grants no permission to mutate what r currently points at.
The fixes in reach order
- Declare the binding
mutwhen the value is owned in this scope and the diagnostic says "not declared as mutable":let mut v, then mutate as written. - Change the reference to
&mutwhen the diagnostic says "behind a&reference": update the function signature (or the method receiver,&selfto&mut self), pass&mutat the call site, and make sure the owner's binding is declaredmut. - Change
&selfto&mut selfwhen a method needs to mutate a field, and declare the receiver valuemutat its binding. - Take the value by value, or return a new value, when the caller does not actually need in-place mutation shared with other code: an owned parameter can be declared
mutinside the function without any reference permission at all. - Use interior mutability —
RefCell<T>orCell<T>— only when a shared reference is genuinely all you can have, for example behind a&selfmethod whose signature cannot change: the value is wrapped so that mutable borrows are checked at runtime instead of compile time.
Traps
- Adding
mutto a binding that holds a shared reference does not work.let mut r = &v;followed byr.push(4);still fails with E0596, behind a&reference — the addedmutonly permits rebindingr, and the compiler even warns that thismutis not needed for the push as written:
fn main() {
let v = vec![1, 2, 3];
let mut r = &v;
r.push(4);
println!("{:?}", v);
}
$ rustc --edition 2021 probe_mut_binding_shared.rs -o probe_mut_binding_shared
warning: variable does not need to be mutable
--> probe_mut_binding_shared.rs:3:9
|
3 | let mut r = &v;
| ----^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default
error[E0596]: cannot borrow `*r` as mutable, as it is behind a `&` reference
--> probe_mut_binding_shared.rs:4:5
|
4 | r.push(4);
| ^ `r` is a `&` reference, so it cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
3 | let mut r = &mut v;
| +++
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0596`.
- Adding
mutto a parameter that holds a shared reference does not work either, for the same reason.fn add_item(mut values: &Vec<i32>)still fails with E0596 inside the body; themutgoverns the local bindingvalues, not the referent, and the compiler warns it is unused:
fn add_item(mut values: &Vec<i32>) {
values.push(4);
}
fn main() {
let v = vec![1, 2, 3];
add_item(&v);
println!("{:?}", v);
}
$ rustc --edition 2021 probe_mut_param.rs -o probe_mut_param
warning: variable does not need to be mutable
--> probe_mut_param.rs:1:13
|
1 | fn add_item(mut values: &Vec<i32>) {
| ----^^^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default
error[E0596]: cannot borrow `*values` as mutable, as it is behind a `&` reference
--> probe_mut_param.rs:2:5
|
2 | values.push(4);
| ^^^^^^ `values` is a `&` reference, so it cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
1 | fn add_item(mut values: &mut Vec<i32>) {
| +++
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0596`.
- Changing only the borrow, without the owner's declaration, swaps one E0596 wording for the other.
let r = &mut v;over avdeclared withoutmutfails as "cannot borrowvas mutable, as it is not declared as mutable" — a mutable reference can only be created from a mutable binding:
fn main() {
let v = vec![1, 2, 3];
let r = &mut v;
r.push(4);
println!("{:?}", r);
}
$ rustc --edition 2021 probe_ref_only_mut.rs -o probe_ref_only_mut
error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable
--> probe_ref_only_mut.rs:3:13
|
3 | let r = &mut v;
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
2 | let mut v = vec![1, 2, 3];
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0596`.
- Shadowing works for an owned local, and only there. Rebinding with a second
let—let mut v = v;— creates a new, mutable binding that owns the value, and the push then compiles:
fn main() {
let v = vec![1, 2, 3];
let mut v = v;
v.push(4);
println!("{:?}", v);
}
$ rustc --edition 2021 probe_shadow.rs -o probe_shadow
$ ./probe_shadow
[1, 2, 3, 4]
mut on the original line is clearer. It is not a fix behind a reference: shadowing a & reference with another binding still leaves a & reference, whose referent you do not own in that scope.
- Interior mutability is the legitimate exception, not a loophole in the wording. The Rust Book chapter on interior mutability defines the pattern as mutating data even when there are immutable references to it, with RefCell<T> enforcing the borrowing rules at runtime instead of compile time — so a value inside a RefCell can be mutated through a shared reference, and breaking the rules there panics at runtime rather than failing compilation:use std::cell::RefCell;
fn main() {
let v = RefCell::new(vec![1, 2, 3]);
let r = &v;
r.borrow_mut().push(4);
println!("{:?}", v.borrow());
}
$ rustc --edition 2021 probe_refcell.rs -o probe_refcell
$ ./probe_refcell
[1, 2, 3, 4]
Reach for this when the shared reference is imposed on you — a trait method taking &self, a structure shared by design. When you control the signature, &mut is the better fix: the permission is stated in the type and checked at compile time, with no runtime borrow state to panic.
- Assignment to a field behind &self is E0594, not E0596. A &self method containing self.count += 1; reports "cannot assign to self.count, which is behind a & reference" under code E0594. The codes differ because assignment and mutable borrowing are different operations in the diagnostics; the underlying missing permission and the &mut self repair are the same.
Decision summary
- The header says "not declared as mutable": add
mutat that binding's declaration — nothing else is missing. - The header says "behind a
&reference": change the reference to&mutwhere it is created — the function signature and call site, the method receiver, or the borrow expression — and declare the owner's bindingmut. - The failing call is a method on a local value (
v.push): the binding wording — check the declaration first. - The failing call is inside a function or method (
values.push,self.entries.push,r.push): the reference wording — check what reference the code holds, not the caller's declaration alone. - A
mutwas added and the same E0596 remains: themutwent to a binding that holds a reference; it permits rebinding, not mutation through the reference. - The signature cannot change and only
&or&selfis available: wrap the mutable state inRefCell<T>orCell<T>and accept runtime borrow checking. - The report is E0594 rather than E0596: a field assignment behind a shared reference — same missing permission, same
&mutrepair, different operation in the diagnostic.
Checklist
- The second clause of the header read first: "not declared as mutable" points at a declaration, "behind a
&reference" points at a reference type. - Every
mutin the fix placed where the diagnostic pointed: on the owner's declaration, or in the reference itself — not on a binding that merely holds a shared reference. - Where a
&mutwas introduced, the signature, the call site, and the owner's declaration all changed together. - Where a method changed to
&mut self, callers checked: the method can no longer be called through a shared reference or on an immutable binding. - Interior mutability used only where a shared reference is imposed, with the runtime panic risk of conflicting borrows understood.
- The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- Fix Rust E0507 "cannot move out of ..." — Vec Index, HashMap Index, a Shared Reference, a Struct Field, and a Mutable Reference
- Fix Rust E0499 "cannot borrow as mutable more than once at a time" — Two Bindings, a Function Call, Two Vec Elements, and a Method Call
- Fix Rust E0515 "cannot return value referencing local variable" — a Local String, a Local Number, a Local Vec Slice, and a Struct Wrapping a Local
- Fix Rust E0502 "cannot borrow as mutable because it is also borrowed as immutable" — End the First Borrow, Split the Work, or Restructure
Sources
- Rust, Error codes index: E0596 — the definition of the error: it occurs because you tried to mutably borrow a non-mutable variable; the erroneous example
let y = &mut x;over an immutable binding, and the fix of making the binding mutable. - The Rust Programming Language, Variables and Mutability — variables are immutable by default, once a value is bound to a name you cannot change it, and
mutin front of the variable name makes it mutable; shadowing creates a new variable rather than mutating the old one. - The Rust Programming Language, References and Borrowing — references are immutable by default like variables, modifying a borrowed value through
&Stringfails with E0596 behind a&reference, and the fix is a mutable reference: the value mademut,&mutat the call, and&mutin the signature. - The Rust Programming Language, RefCell
and the Interior Mutability Pattern — interior mutability allows mutating data even when there are immutable references to it,RefCell<T>enforces the borrowing rules at runtime rather than compile time, and the value inside aRefCell<T>can be mutated even when theRefCell<T>itself is immutable. - The Rust Reference, Operator expressions — the borrow operators: for a shared borrow (
&) the place may not be mutated but may be read or shared again, and a mutable borrow (&mut) is evaluated in a mutable place context and is the borrow that carries permission to mutate.
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.