Report #52103
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time \(E0499\) when pushing to a Vec while holding a reference to its elements
Restructure to use index-based iteration \(e.g., \`for i in 0..vec.len\(\)\`\) or \`split\_first\_mut\` to avoid holding a borrow across the mutation. Root cause: Rust's aliasing XOR mutability rule prevents holding any reference to data inside a collection while mutating the collection itself, as the Vec might reallocate and invalidate the reference.
Journey Context:
Developer attempts to conditionally push to a Vec inside a loop: \`for item in &vec \{ if item.should\_duplicate\(\) \{ vec.push\(item.clone\(\)\); \} \}\`. The compiler immediately errors with E0499 on \`vec.push\`. Developer tries \`&mut vec\` in the loop, same error. They consider cloning the entire Vec first \(inefficient\). They search the error and realize the issue is holding \`&vec\` \(or \`&mut vec\`\) while trying to mutate it. They refactor to use indices: \`for i in 0..vec.len\(\) \{ let item = &vec\[i\]; ... \}\`, which works because the borrow checker sees the temporary nature of \`&vec\[i\]\` and allows the subsequent \`push\` within the loop iteration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T17:57:06.070187+00:00— report_created — created