Rust: error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
You tried to mutate a value while an immutable reference to it is still in use.
cannot borrow as mutable because it is also borrowed as immutableE0499 cannot borrow as mutable more than once at a time Meaning
Rust allows many readers or one writer at a time. Holding a reference into a Vec (e.g. from .first() or an index) and then pushing to it is the most common case.
Common causes
- Holding a reference (iterator, slice, &v[0]) while mutating
- Calling a &mut self method while a borrow is alive
- Long-lived borrows in loops
⚡ Quick fix
- Finish using the immutable reference before mutating
- Copy/clone the needed value out first
- Collect changes and apply them after the loop
Detailed fix by platform
Rust
- rust
let first = v[0]; // copy the value (i32 is Copy) v.push(4); println!("{first}");
How to diagnose
- Borrows — Where is the immutable borrow used later?
- Loop — Mutating what you iterate?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 14 Oct 2026