Report #103440
[bug\_fix] error\[E0499\]: cannot borrow \`items\` as mutable more than once at a time
Collect the changes into a separate Vec first, then apply them after the iteration finishes \(e.g., \`items.extend\(to\_add\);\`\). Alternatively, use \`iter\_mut\(\)\` if you are modifying elements in place, or iterate over indices backwards if you must remove elements.
Journey Context:
You write a loop that reads a Vec and conditionally pushes new elements into the same Vec. The compiler rejects it with E0499. You first think the borrow checker is being paranoid because the push only happens inside an \`if\`. You try shrinking scopes, adding explicit braces, or using \`&items\` versus \`items.iter\(\)\` — none of it helps. Then you realize the iterator itself holds a shared borrow over the whole Vec for the entire lifetime of the loop, so any mutable borrow — even a single \`push\` — would create aliasing plus mutation simultaneously. That overlap is exactly what Rust forbids to prevent iterator invalidation \(the underlying allocation could reallocate on push and leave the iterator pointing at freed memory\). The fix works because it splits the operation into a read-only phase and a write phase that never overlap: you gather what to add while the shared borrow is active, release the iterator, and only then mutate the Vec.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:24:16.860550+00:00— report_created — created