Report #5911
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time
Restructure the loop to collect values first, iterate by index in reverse, or explicitly \`drop\(\)\` the borrow before the mutable operation. The root cause is that holding any reference \(even shared\) into a collection extends the borrow for the entire lexical scope of the loop body, preventing any simultaneous mutable borrow.
Journey Context:
Developer iterates over a Vec with \`for item in &vec\` and tries to \`vec.push\(\)\` inside the loop. The compiler errors with a double mutable borrow. They try \`for item in &mut vec\` but still fail because the iterator holds a mutable borrow for the whole loop body. They inspect the borrow with \`println\!\` and realize the borrow lasts until the end of the iteration. They consider cloning the data but that is expensive. They discover that collecting the values to push into a separate Vec first, then extending after the loop, works. Alternatively, they learn that iterating by index in reverse \(\`for i in \(0..vec.len\(\)\).rev\(\)\`\) avoids the borrow issue because the index doesn't hold a reference to the collection itself. The fix works because it ensures no borrow is active when the mutable operation occurs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T22:39:28.703919+00:00— report_created — created