Report #97182
[bug\_fix] error\[E0499\]: cannot borrow \`items\` as mutable more than once at a time
Do not mutate a Vec while an iterator borrows it. Collect the new elements into a separate Vec first, then call \`items.extend\(new\_items\)\` after the loop. Alternatively, use \`for i in 0..items.len\(\)\` with index-based access, or use \`Vec::retain\` / \`Vec::drain\_filter\` if you are removing elements. The immutable iterator holds a borrow of the whole collection until it drops, so appending would invalidate its view.
Journey Context:
You are building a batch processor. You write \`for item in &items \{ if item.expired\(\) \{ items.push\(replacement\(item\)\); \} \}\` and Rust refuses to compile. You stare at the loop: you are only reading \`item\`, so why does the compiler complain about a mutable borrow? You add \`&mut items\` to the iterator and get a different error. You search the error code and read that a shared iterator borrows the entire Vec for its whole lifetime, even though it yields one element at a time; pushing could reallocate the backing buffer, invalidating the iterator's pointer into it. You refactor by collecting replacements into \`let mut new\_items = Vec::new\(\)\` inside the loop and \`items.extend\(new\_items\)\` afterward. The compile error disappears and the logic is actually clearer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T04:41:29.555005+00:00— report_created — created