Report #14689
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time
Collect indices or items into a temporary collection first, then process modifications after the iteration, or use \`Vec::retain\` if removing items. This separates the read phase \(iteration\) from the write phase \(mutation\), eliminating the simultaneous borrow.
Journey Context:
Developer writes a loop \`for item in &vec\` to scan elements, then inside the loop tries \`vec.push\(new\_item\)\` based on some condition. The compiler immediately halts with the mutable borrow error. Developer tries wrapping the Vec in \`RefCell\`, but then panics at runtime. They search StackOverflow and realize the iterator holds an immutable borrow of the entire Vec for the whole loop duration, so no mutation is possible. They try collecting indices into a \`Vec\` first, then looping over those indices to push new elements to a separate Vec, then extending the original. Finally, they realize they can simply collect new items into a temporary Vec during the scan, then \`vec.extend\(temp\)\` after the loop, which satisfies the borrow checker by splitting the phases.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:14:33.208381+00:00— report_created — created