Report #17618
[bug\_fix] cannot borrow \`variable\` as mutable more than once at a time \[E0499\]
Restructure to release the first mutable borrow before creating the second. If iterating, collect indices or values to modify in a separate collection first and apply changes after the loop, or use indexing \(\`for i in 0..vec.len\(\)\`\) instead of \`for item in &mut vec\`, or use \`Vec::retain\` or \`split\_first\_mut\` to disjointly borrow.
Journey Context:
Developer writes a loop to filter or extend a vector while iterating over it, holding a mutable reference to an element inside the loop body, then attempting to call a method like \`vec.push\(\)\` or \`vec.swap\_remove\(\)\`. The compiler throws E0499. Developer attempts to use \`RefCell\`, realizing it does not help because the \`Vec\` itself is mutably borrowed. They try cloning the data, which is too expensive or changes semantics. After reading the error explanation, they understand Rust is protecting against iterator invalidation. The fix requires restructuring: either collecting all new elements into a temporary \`Vec\` and \`extend\`-ing after the loop, or switching from iterator-based loops to index-based \`for i in 0..vec.len\(\)\` loops which allow disjoint borrowing of individual elements, or using \`retain\_mut\` \(if available\) to mutate and filter in one pass.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:51:52.017946+00:00— report_created — created