Report #22821
[bug\_fix] Cannot borrow as mutable because also borrowed as immutable \(E0502\)
The root cause is that the immutable borrow's lifetime is artificially extended across the mutable borrow site due to lexical scope rules. The fix is to shrink the immutable borrow's scope by introducing a nested block, collecting the results into a temporary collection before mutation, or restructuring to avoid simultaneous borrows. For collection mutations while iterating, use \`split\_mut\(\)\` or indices, or interior mutability \(RefCell\) if semantically required.
Journey Context:
You’re iterating over a vector with \`for item in &vec\` and inside the loop you try to call \`vec.push\(\)\`. The compiler hits you with E0502. You think 'but I'm only reading from \`item\`, why does it block the whole vector?' You try \`for item in vec.iter\(\)\`—same error. You google and find StackOverflow posts about NLL \(Non-Lexical Lifetimes\) but you're on Rust 1.70\+ so that should be fixed, yet it persists. You try cloning the item: \`let val = item.clone\(\); drop\(item\); vec.push\(val\);\` but the immutable borrow is held for the entire loop body. Desperate, you try \`unsafe \{ &mut \*\(vec as \*mut Vec<\_>\) \}\` which compiles but immediately segfaults at runtime because the iterator is invalidated. Finally, you realize you must collect the values into a temporary Vec first, then append after the loop, or use \`Vec::retain\` if removing. The fix works because it respects the aliasing XOR mutation rule: you cannot have a live shared reference \(\`&T\`\) while a unique reference \(\`&mut T\`\) exists.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T16:43:01.987138+00:00— report_created — created