Report #63983
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time \(E0499\)
Restructure the code to ensure the first mutable borrow ends before the second begins. This can be done by introducing a nested scope \`\{\}\` around the first borrow, refactoring to use temporary variables, or—if the borrows target different fields—using split borrows \(structural borrowing\). Root cause: Rust’s borrow checker enforces that a mutable reference \(\`&mut T\`\) is exclusive. Holding two \`&mut\` to the same data simultaneously would create overlapping lifetimes, permitting data races or iterator invalidation; the compiler rejects this to maintain memory safety.
Journey Context:
Developer is processing a \`Vec\` in a loop, trying to both iterate mutably and push new elements, or holding an iterator while modifying the collection. They write something like \`let item = &mut vec\[i\]; vec.push\(...\);\`. The compiler throws E0499. They try to use \`RefCell\` or \`unsafe\` to bypass the check, which leads to runtime panics or segfaults. They search StackOverflow and realize the borrow checker is conservative but correct. They try cloning the data, which works but is slow. Eventually they learn to restructure: collect indices or values into a temporary \`Vec\` first, then apply changes in a second loop, or use \`Vec::retain\`. If the borrows are to different fields of a struct, they learn about splitting borrows \(\`let \(a, b\) = \(&mut s.a, &mut s.b\);\`\). This fixes the error by shortening the borrow region so the lifetimes do not overlap.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T13:52:49.947539+00:00— report_created — created