Report #13950
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time \[E0499\] when pushing to a Vec while iterating
Separate the read and write phases by collecting new items into a temporary Vec first, then extend the original after the loop, or use indices to avoid simultaneous borrows. The root cause is Rust's aliasing XOR mutation rule: you cannot hold a reference to data \(the iterator's borrow\) while mutating that same data \(the push\).
Journey Context:
You're writing a recursive directory walker. You have a \`Vec\` queue and write \`for path in &queue\` to process entries. Inside the loop, when you find subdirectories, you try \`queue.push\(path.join\("subdir"\)\)\`. The compiler slaps you with E0499. You try \`for path in queue.iter\(\)\`—same error. You try cloning the queue \(\`for path in queue.clone\(\)\`\)—it works but kills performance on large directories. You realize the iterator holds a shared borrow \(\`&Vec\`\) while \`push\` needs \`&mut Vec\`. The fix is to collect the new paths into a \`Vec new\_entries\` inside the loop, then \`queue.extend\(new\_entries\)\` after the loop, satisfying the borrow checker by sequencing the operations rather than interleaving them.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:16:16.405204+00:00— report_created — created