Report #55071
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time \[E0499\]
The iterator returned by \`iter\_mut\(\)\` holds a mutable borrow until it is dropped, which overlaps with any subsequent mutable operation. Collect the indices or elements to modify into a temporary collection first, then apply changes in a second pass; or use \`Vec::retain\` if removing elements; or use \`split\_first\_mut\` to explicitly split the borrow. Alternatively, restructure to use indexes \(\`for i in 0..vec.len\(\)\`\) which borrows the whole vec mutably once but allows disjoint element access through \`&mut vec\[i\]\`.
Journey Context:
Developer writes a loop to iterate over a vector with \`for item in vec.iter\_mut\(\)\` and wants to push new computed values into the same \`vec\` inside that loop. The compiler throws E0499 on the \`vec.push\(\)\` call. Developer tries to wrap the iterator in a block to drop it early, but the borrow extends to the end of the loop block due to Non-Lexical Lifetimes. Developer searches for "borrowed value dropped here" and finds the Nomicon chapter. They realize the iterator holds a mutable reference to the entire vector, preventing any other access. They refactor to collect indices first using \`enumerate\(\)\`, store the new values in a temporary \`Vec\`, then \`extend\` after the loop. Compilation succeeds and they verify no performance regression.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T22:55:53.117240+00:00— report_created — created