error[E0502]: cannot borrow as mutable because it is also borrowed as immutable
You tried to change a value (a mutable borrow) while a shared reference to it was still in use. Rust allows many readers or one writer at a time, never both at once.
Quick fix: Copy or clone the data you read, or finish using the reference before you modify the value.
Code that triggers E0502
fn main() {
let mut queue = vec![1, 2, 3];
let first = &queue[0];
queue.push(4);
println!("first: {first}");
}What the compiler says
error[E0502]: cannot borrow `queue` as mutable because it is also borrowed as immutable
--> src/main.rs:4:5
|
3 | let first = &queue[0];
| ----- immutable borrow occurs here
4 | queue.push(4);
| ^^^^^^^^^^^^^ mutable borrow occurs here
5 | println!("first: {first}");
| ------- immutable borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0502`.Why Rust raises E0502
first borrows queue immutably, and queue.push(4) needs a mutable borrow. The rule isn’t red tape here: push may need more room and move the whole vector to a bigger allocation. first would then point at the old, freed buffer.
The conflict is real only because first is used again on line 5, after the push. A borrow lasts until its last use, so the order of your statements decides whether two borrows overlap.
The most common real-world version is changing a collection while you loop over it. The for loop keeps scores borrowed for the whole loop, so push inside it is rejected:
fn main() {
let mut scores = vec![40, 75, 90];
for s in &scores {
if *s > 50 {
scores.push(s + 5);
}
}
println!("{scores:?}");
}error[E0502]: cannot borrow `scores` as mutable because it is also borrowed as immutable
--> src/main.rs:5:13
|
3 | for s in &scores {
| -------
| |
| immutable borrow occurs here
| immutable borrow later used here
4 | if *s > 50 {
5 | scores.push(s + 5);
| ^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0502`.How to fix E0502
1. Copy the value instead of borrowing it
queue[0] is an i32, which is Copy. Take the number itself and nothing stays borrowed.
fn main() {
let mut queue = vec![1, 2, 3];
let first = queue[0];
queue.push(4);
println!("first: {first}");
}2. Finish with the reference before you modify
Reorder the code so the last use of first comes before push. The shared borrow ends on line 4, and the mutation is allowed.
fn main() {
let mut queue = vec![1, 2, 3];
let first = &queue[0];
println!("first: {first}");
queue.push(4);
}3. For loops: collect first, then change
Build the new items in a separate collection while you read, then add them after the loop with extend. Reading and writing no longer overlap.
fn main() {
let mut scores = vec![40, 75, 90];
let bonus: Vec<i32> = scores.iter().filter(|&&s| s > 50).map(|s| s + 5).collect();
scores.extend(bonus);
println!("{scores:?}");
}Related errors
- E0499: cannot borrow as mutable more than once at a time
- E0596: cannot borrow as mutable, as it is not declared as mutable
- E0505: cannot move out of value because it is borrowed
Learn the concept behind it
The References and Borrowing lesson explains the rules behind E0502 with diagrams and more examples. For the official reference, run rustc --explain E0502 or read the E0502 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.