Report #88677
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time \[E0499\]
Restructure the code to avoid simultaneous mutable and immutable borrows. If modifying a collection while iterating, collect the modifications into a separate collection first, then apply them after the iteration. Alternatively, use \`Vec::retain\` for removals, or use indices \(\`for i in 0..vec.len\(\)\`\) with careful logic. Root cause: Rust enforces aliasing XOR mutation at all times to prevent data races; holding an iterator \(immutable borrow\) invalidates any simultaneous mutable borrow of the same data.
Journey Context:
The developer writes a loop like \`for item in &vec\` and tries to call \`vec.push\(...\)\` inside the loop body to append derived values. The compiler flags the \`push\` call with E0499. The developer first suspects a compiler bug or tries to circumvent the checker by wrapping the Vec in a \`RefCell\` \(which fails because \`RefCell\` itself would need a mutable borrow\). They then try cloning the entire Vec \(expensive and often impractical for large data\). After searching the error code, they realize the iterator itself holds a borrow, so they refactor to first collect new items into a temporary \`Vec\`, then \`extend\` the original after the loop, breaking the temporal overlap of borrows.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T07:25:57.432712+00:00— report_created — created