E0506 Assign While Borrowed Fix
A borrow of a value is still live - and still has a use ahead of it - at the line where an assignment tries to replace that value. This guide separates four shapes that produce E0506 and matches each one to the change that ends or avoids the overlap: finish with the borrow first, scope it, copy out of it, or assign after the borrowing loop.
Written November 23, 2026 · Tested with Rust 1.99.1 · 21 min read
- The rule underneath: a value cannot be replaced while a borrow of it is still live
- How to read the diagnostic
- The first failure: a variable reassigned while its shared borrow is still used later
- The second failure: a struct field assigned while that field is borrowed
Written November 23, 2026
Tested with Rust 1.99.1
What this guide is
Error E0506 is a blocked replacement. The error codes index defines it in one line: an attempt was made to assign to a borrowed value. The assignment itself is the plain kind - x = 20, p.x = 10, v = vec![9, 9, 9] - and the value on its left-hand side is not behind a reference and is declared mut. What blocks it is a borrow: some reference to that same value was created earlier, is still live at the assignment line, and is used again afterward. Replacing the value would invalidate that reference, so the compiler refuses the assignment until the borrow is over.
The four forms in this guide each reproduce E0506 with a small, complete program, and each holds the borrow open by a different route. A shared borrow of a variable used after the variable is reassigned. A borrow of one struct field while that same field is assigned. An assignment to a vector inside a loop that is iterating over that vector. And a whole-vector replacement while a borrow of one of its elements is still live. In each family the failing program is small, the full compiler output is shown, and the fix ends the overlap rather than fighting it: the borrow's last use moves before the assignment, the borrowed value is copied out so no borrow survives, or the assignment moves to after the loop.
Two neighboring codes are not E0506 and are worked only in the traps and the decision summary. When the assignment needs a mutable borrow of the borrowed value - assigning to one element of a vector through its index, for example - the error is E0502, cannot borrow as mutable because it is also borrowed as immutable, because indexed assignment borrows the vector mutably instead of simply replacing a place. And when the assignment writes through a shared reference - *r += 1 where r is a & borrow - the error is E0594, cannot assign, because the place sits behind a reference that provides no write access at all. E0506 is the remaining case: the place is directly assignable, and only the still-live borrow stands in the way.
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: a value cannot be replaced while a borrow of it is still live
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 frames the core restriction around mutable references - while one exists, no other reference to the same value may be used - and the same protection runs in the other direction for assignment. A shared borrow promises its holder that the value it points at will stay the value it was borrowed from. A plain assignment replaces that value wholesale: the old contents are dropped and new contents take their place. If any borrow of the value is still live, the replacement would leave that borrow pointing at a value its holder never borrowed - or, for a dropped heap value, at memory that is no longer the value at all.
What makes a borrow "still live" is use, not scope alone. The Book states the scope rule directly: a reference's scope starts where it is introduced and continues through the last time that reference is used. This is the non-lexical-lifetimes behavior every fix in this guide relies on. A borrow whose last use is on the line before the assignment is already over at the assignment, and the identical assignment compiles. A borrow with one more use waiting below the assignment - a println!, a loop that will read it again - is live across the assignment, and the assignment fails. That is why the diagnostic always shows three points, not one: where the value was borrowed, where it is assigned, and where the borrow is later used. Delete or move that later use and the error goes with it.
The rule follows the place, at the granularity of the place. Borrowing one field of a struct borrows that field: assigning to a different field of the same struct is a disjoint place and compiles while the first field's borrow is live, as the traps section shows. Borrowing the whole value, or borrowing one field and then assigning to the whole value, overlaps everywhere, and assigning to the borrowed field itself overlaps exactly. The compiler tracks places - a variable, a field path, a whole value - and E0506 fires only where the assigned place and the borrowed place overlap.
Assignment is also narrower than it looks. A plain = to a variable, a field, or a whole value replaces the place directly and is the operation E0506 guards. Operations that must borrow the place mutably in order to write - indexed assignment v[0] = 99, which goes through a mutable index borrow, or a mutating method call such as push - are borrow operations, and a live shared borrow blocks them with E0502 instead. The traps section shows the same vector failing both ways: v[0] = 99 under a live element borrow is E0502, and v = vec![9, 9, 9] under the same borrow is E0506. Reading the code in the header tells you which operation you actually wrote.
How to read the diagnostic
The header names the assigned place and the reason in one line: "cannot assign to" the place "because it is borrowed". Read the place first, because it tells you what is being replaced - x is a whole variable, p.x is one field, p or v is a whole struct or vector. Three labeled lines then map the overlap. The line ending "x is borrowed here" marks where the live borrow was created. The caret line repeats the assignment with the label "x is assigned to here but it was already borrowed". And a later line is marked "borrow later used here" - that use is the reason the borrow is still live, and it is the line every fix in this guide moves, removes, or works around.
There is no help: rewrite in an E0506 diagnostic, unlike the borrow-kind errors: the compiler does not propose changing a signature or adding mut, because neither is the problem. The owning binding is already mut - without mut the assignment would fail earlier, for a different reason. The fix is always temporal or structural: end the borrow before the assignment (move its last use up, or confine it to a scope block), remove the borrow (copy the value out of it, for a type that is Copy or cheap to clone), split the places so they no longer overlap, or move the assignment after the construct that holds the borrow, such as a loop. One family below carries an extra note - "borrow occurs due to deref coercion" - where the borrow is of a vector's slice contents through iter(); the note explains how the borrow arises, and the fix is unchanged.
The first failure: a variable reassigned while its shared borrow is still used later
r borrows x, the next line replaces x, and the println! after that still reads through r. That later read is what keeps the borrow alive across the assignment:
fn main() {
let mut x = 10;
let r = &x;
x = 20;
println!("{}, {}", r, x);
}
$ rustc --edition 2021 fail_scalar.rs -o fail_scalar
error[E0506]: cannot assign to `x` because it is borrowed
--> fail_scalar.rs:4:5
|
3 | let r = &x;
| -- `x` is borrowed here
4 | x = 20;
| ^^^^^^ `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`.
The three labels are the whole story: borrowed on line 3, assigned on line 4, used again on line 5. The fix needs no new construct - read the borrow for the last time before the assignment, and the borrow ends at that read:
fn main() {
let mut x = 10;
let r = &x;
println!("{}", r);
x = 20;
println!("{}", x);
}
$ rustc --edition 2021 fix_scalar.rs -o fix_scalar
$ ./fix_scalar
10
20
The output is the pair the failing program was trying to print, in the only order that works: 10, the borrowed value read while the borrow was live, then 20, the replacement value after the assignment the borrow no longer blocks. Nothing about the borrow changed - it is the same &x - only its last use moved above the assignment.
The second failure: a struct field assigned while that field is borrowed
Here the borrowed place and the assigned place are the same field. rx borrows p.x, the next line assigns to p.x, and the final println! still reads rx:
struct Point {
x: i32,
y: i32,
}
fn main() {
let mut p = Point { x: 1, y: 2 };
let rx = &p.x;
p.x = 10;
println!("{}, {}", rx, p.y);
}
$ rustc --edition 2021 fail_field.rs -o fail_field
error[E0506]: cannot assign to `p.x` because it is borrowed
--> fail_field.rs:9:5
|
8 | let rx = &p.x;
| ---- `p.x` is borrowed here
9 | p.x = 10;
| ^^^^^^^^ `p.x` is assigned to here but it was already borrowed
10 | println!("{}, {}", rx, p.y);
| -- borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0506`.
The header names the field path p.x, not the whole struct - the borrow and the assignment meet on that one field. The fix is the first family's fix at field granularity: the borrow's last use moves above the assignment:
struct Point {
x: i32,
y: i32,
}
fn main() {
let mut p = Point { x: 1, y: 2 };
let rx = &p.x;
println!("{}", rx);
p.x = 10;
println!("{}, {}", p.x, p.y);
}
$ rustc --edition 2021 fix_field.rs -o fix_field
$ ./fix_field
1
10, 2
The output is 1, the borrowed field read before the replacement, then 10, 2 - the field after assignment, and the y field the program never touched. Note what the granularity buys in the other direction: had the assignment been to p.y instead, no fix would have been needed at all - a borrow of p.x and an assignment to p.y are disjoint places, and that program compiles as written in the traps section.
The third failure: assigning to a vector inside a loop that iterates over it
A for loop over v.iter() borrows the vector for the loop's whole duration - the iterator holds the borrow, and every pass through the body is inside it. Assigning to v in the body is an assignment under a borrow that the next pass will use again:
fn main() {
let mut v = vec![1, 2, 3];
for x in v.iter() {
println!("{}", x);
v = vec![9, 9, 9];
}
println!("{:?}", v);
}
$ rustc --edition 2021 fail_loop_iter_assign.rs -o fail_loop_iter_assign
error[E0506]: cannot assign to `v` because it is borrowed
--> fail_loop_iter_assign.rs:5:9
|
3 | for x in v.iter() {
| --------
| |
| `v` is borrowed here
| borrow later used here
4 | println!("{}", x);
5 | v = vec![9, 9, 9];
| ^ `v` is assigned to here but it was already borrowed
|
= note: borrow occurs due to deref coercion to `[i32]`
note: deref defined here
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3848:5
|
3848 | type Target = [T];
| ^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0506`.
The borrow label sits on the loop header itself, marked both "borrowed here" and "later used here", because the iterator is created there and consumed across every pass. There is no single last use to move up - the borrow's uses are the loop's remaining passes - so the fix moves the assignment instead, to after the loop, where the iterator and its borrow are finished:
fn main() {
let mut v = vec![1, 2, 3];
for x in v.iter() {
println!("{}", x);
}
v = vec![9, 9, 9];
println!("{:?}", v);
}
$ rustc --edition 2021 fix_iter_assign.rs -o fix_iter_assign
$ ./fix_iter_assign
1
2
3
[9, 9, 9]
The output is the three elements the loop printed, then [9, 9, 9], the replacement vector. If the new value has to be computed from the loop's work, compute it into a separate variable inside the loop and assign it to v after the loop ends - the borrow blocks replacing v, not building its replacement elsewhere.
The fourth failure: replacing a whole vector while a borrow of one element is live
first borrows v[0] - one element - and the next line replaces the whole vector. The assigned place is v itself, and a borrow of any part of a value is a borrow of the value for a whole-value replacement: the new vector's elements are different places from the old one's, so the element borrow cannot survive the swap:
fn main() {
let mut v = vec![1, 2, 3];
let first = &v[0];
v = vec![9, 9, 9];
println!("{}, {:?}", first, v);
}
$ rustc --edition 2021 fail_vec_whole.rs -o fail_vec_whole
error[E0506]: cannot assign to `v` because it is borrowed
--> fail_vec_whole.rs:4:5
|
3 | let first = &v[0];
| - `v` is borrowed here
4 | v = vec![9, 9, 9];
| ^ `v` is assigned to here but it was already borrowed
5 | println!("{}, {:?}", first, v);
| ----- borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0506`.
The borrow label names v, not v[0] - for a whole-value assignment, the element borrow registers as a borrow of the vector. Here the value being read through the borrow is an i32, which is Copy, so the cleanest fix removes the borrow entirely: copy the element out into an owned variable, and no reference survives to block the replacement:
fn main() {
let mut v = vec![1, 2, 3];
let first = v[0];
v = vec![9, 9, 9];
println!("{}, {:?}", first, v);
}
$ rustc --edition 2021 fix_vec_copy.rs -o fix_vec_copy
$ ./fix_vec_copy
1, [9, 9, 9]
The output is the value the failing program was trying to print: 1, [9, 9, 9] - the copied first element, then the replacement vector. The one-character change, &v[0] to v[0], is the whole fix: first now owns a copy of the number instead of borrowing the place it lives in. For an element type that is not Copy, the same shape works with an explicit clone() where the copy is worth its cost - and where it is not, the first family's fix applies instead: read through the borrow for the last time before the assignment line.
The fixes in reach order
- The borrow has a last use that can move: move it above the assignment - the borrow ends at its last use, and the assignment after it is free. This is the first reach for a named borrow in straight-line code.
- The borrow is used in one self-contained block of code: confine it to a scope block
{ ... }, as the error index's own example does and the traps section shows - the borrow ends at the closing brace whatever its last use inside was. - The code only needs the borrowed value, not the place: copy it out - a plain read for a
Copytype,clone()otherwise - and keep no reference at all. - The assigned place and the borrowed place are different fields of one struct: do nothing - disjoint field places do not overlap, and the program already compiles; check the header's place path before restructuring.
- The borrow is held by a loop, an iterator, or a call that outlasts the assignment line: move the assignment after that construct, or compute the replacement into a separate variable inside it and assign afterward.
- The value must genuinely be read and replaced in interleaved order with no restructuring available: that is the shape interior mutability exists for - hold the value in a
RefCelland borrow it at each use, accepting a runtime borrow check in place of the compile-time one.
Traps
- A scope block ends a borrow even when moving a use is awkward. This is the first family with the borrow and its only use inside braces - the error index's suggested shape - and it compiles as written:
fn main() {
let mut x = 10;
{
let r = &x;
println!("{}", r);
}
x = 20;
println!("{}", x);
}
$ rustc --edition 2021 probe_scope.rs -o probe_scope
$ ./probe_scope
10
20
10, read inside the block, then 20 after the assignment. The block is the blunt form of the last-use fix: it guarantees the borrow is over at a line you choose, at the cost of an extra level of nesting - worth it when the borrow's uses are spread through code that cannot be reordered.
- Assigning to a different field than the borrowed one is not E0506 at all. This program borrows p.x and assigns to p.y while the borrow is live and used afterward, and it compiles:struct Point {
x: i32,
y: i32,
}
fn main() {
let mut p = Point { x: 1, y: 2 };
let rx = &p.x;
p.y = 20;
println!("{}, {}", rx, p.y);
}
$ rustc --edition 2021 probe_split.rs -o probe_split
$ ./probe_split
1, 20
The output is 1, 20: the borrowed x read after the assignment, and the assigned y. Places are tracked per field, so a field borrow blocks that field and the whole value - not its sibling fields. Before restructuring around an E0506, read the place in the header: if the code you need to write assigns a sibling field, it may already be legal.
- Compound assignment to the variable itself is still E0506. x += 1 reads and replaces x in place, and under a live borrow it fails with the same code as x = 20 - the operation is an assignment to the borrowed place, not a mutable borrow of it:
fn main() {
let mut x = 10;
let r = &x;
x += 1;
println!("{}, {}", r, x);
}
$ rustc --edition 2021 probe_compound.rs -o probe_compound
error[E0506]: cannot assign to `x` because it is borrowed
--> probe_compound.rs:4:5
|
3 | let r = &x;
| -- `x` is borrowed here
4 | x += 1;
| ^^^^^^ `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`.
Same three labels, same fix: the borrow's last use - the println! - is what holds it open, not the form of the assignment.
- Assigning to one element of the same vector is a different error. This program is the fourth family with the whole-vector replacement swapped for an element assignment, and the code changes: v[0] = 99 writes through a mutable index borrow of v, so a live shared borrow blocks it as a borrow conflict, E0502:
fn main() {
let mut v = vec![1, 2, 3];
let first = &v[0];
v[0] = 99;
println!("{}, {:?}", first, v);
}
$ rustc --edition 2021 fail_vec_elem.rs -o fail_vec_elem
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
--> fail_vec_elem.rs:4:5
|
3 | let first = &v[0];
| - immutable borrow occurs here
4 | v[0] = 99;
| ^ mutable borrow occurs here
5 | println!("{}, {:?}", first, v);
| ----- immutable borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0502`.
The header says "cannot borrow as mutable", the caret is a single column under the index expression, and the labels say "mutable borrow occurs here" where E0506's say "assigned to here". The practical difference: E0502 is about a second borrow of any kind - a &mut, a mutating method call, an indexed write - while E0506 is about a direct replacement of the place. The fixes overlap (end the first borrow first), but the code in the header tells you which operation to look for in your own code. The error codes index defines E0502 as a variable borrowed again with a different mutability.
- Writing through the shared reference is a third error, not this one. Here nothing is assigned to x itself - the assignment writes to *r, the place behind the borrow - and the failure is E0594, because a shared reference provides no writable place at all:
fn main() {
let x = 10;
let r = &x;
*r += 1;
println!("{}", x);
}
$ rustc --edition 2021 probe_e0594.rs -o probe_e0594
error[E0594]: cannot assign to `*r`, which is behind a `&` reference
--> probe_e0594.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 separation is exact: assign to the borrowed value itself while it is borrowed - E0506; assign through a shared reference to the value behind it - E0594, whose fix is a &mut borrow, the rewrite its help proposes. The error codes index defines E0594 as a non-mutable value being assigned a value. In E0506 the binding is already mut and no signature change is proposed; in E0594 the borrow kind is the defect.
Decision summary
- The header says "cannot assign to" a place "because it is borrowed", with labels for where it was borrowed and where the borrow is later used: the operation is a plain assignment and a borrow of the same place is still live - end the borrow, copy out of it, or move the assignment.
- The place in the header is a field path such as
p.x: that field is the borrowed place - the same fixes apply at field granularity, and sibling fields are not blocked. - The borrow label sits on a loop header or an iterator: the borrow lasts as long as the loop - move the assignment after the loop, or build the replacement inside the loop and assign it afterward.
- The place is a whole variable or value and the borrow is of one part of it - an element, a field: a whole-value replacement overlaps every part - copy the part's value out, or finish with the borrow before replacing.
- The header instead says "cannot borrow as mutable because it is also borrowed as immutable" (E0502): the failing operation needed a mutable borrow - an indexed assignment, a mutating method call, an explicit
&mut- not a plain assignment. - The header instead says the place "is behind a
&reference" (E0594): the assignment writes through a shared reference - the fix is a mutable borrow on that path, not a reordering. - The report is E0499 instead: two mutable borrows overlap - no assignment is involved at all; the error codes index defines it as a variable borrowed as mutable more than once.
Checklist
- The header read for the assigned place: a whole variable, a field path, or a whole value - the place fixes which borrows can overlap it.
- The three labels traced: where the place was borrowed, the assignment line, and the later use that keeps the borrow live.
- The later use dealt with first: moved above the assignment, removed, or replaced by a copy of the borrowed value taken before the assignment.
- Field places checked for disjointness before restructuring: a sibling field's assignment needs no fix.
- Loop and iterator borrows treated as lasting to the end of the construct: the assignment placed after it, or the replacement built separately inside it.
- The neighboring codes ruled out by the header's wording: a mutable-borrow operation is E0502, an assignment through a shared reference is E0594 - neither is fixed by reordering alone in the way E0506 is.
- The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- E0503 Cannot Use Fix
- E0505 Cannot Move Out Fix
- E0594 Cannot Assign Fix
- 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: E0506 - the definition of the error: an attempt was made to assign to a borrowed value; its example assigns a whole struct value while a reference to it is held, and its fixes limit the borrow's lifetime with a scoped block or end the borrow by passing the reference into a function.
- The Rust Programming Language, References and Borrowing - references borrow a value without owning it; a mutable reference excludes every other reference to the same value; and a reference's scope runs from its introduction through its last use, so a borrow whose last use precedes a later operation no longer blocks it.
- Rust, Error codes index: E0502 - the neighboring mutable-borrow error: a variable already borrowed was borrowed again with a different mutability; the distinction from E0506 is that the blocked operation is a borrow - an indexed assignment, a mutating method call, an explicit
&mut- rather than a plain assignment. - Rust, Error codes index: E0594 - the neighboring behind-a-reference error: a non-mutable value was assigned a value; the distinction from E0506 is that the assigned place sits behind a shared reference or an immutable binding, rather than being a directly assignable place blocked only by a live borrow.
- Rust, Error codes index: E0499 - a variable was borrowed as mutable more than once; cited only to separate a report with no assignment in it from E0506.
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.