Report #66713
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time
Collect items to add into a temporary Vec, then extend after the loop, or use index-based iteration with \`while\` or \`for i in 0..vec.len\(\)\` to avoid holding a borrow across the mutation.
Journey Context:
Developer is building a graph algorithm with a \`Vec\`. They write \`for node in &mut nodes \{ if condition \{ nodes.push\(child\); \} \}\` to dynamically add children while traversing. The compiler throws E0499. They try \`tasks.iter\_mut\(\).for\_each\(\)\` but the borrow persists. They attempt to use \`RefCell>\` hoping for interior mutability, but learn that \`RefCell\` panics at runtime and doesn't solve the fundamental aliasing rule. They search StackOverflow and find that the iterator holds a mutable borrow for the entire loop body. The epiphany comes when they realize they can decouple the iteration from the mutation: either iterate by index \`for i in 0..nodes.len\(\)\` \(which doesn't borrow the vec itself, just the length\), or collect new nodes into a \`Vec new\_nodes\` inside the loop, then \`nodes.extend\(new\_nodes\)\` after the loop. This works because the temporary vec is a separate allocation, and \`extend\` only requires a mutable borrow of \`nodes\` after the immutable borrows from the iteration are dropped.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T18:27:34.573405+00:00— report_created — created