Report #83809
[bug\_fix] cannot borrow \`vec\` as mutable because it is also borrowed as immutable \[E0502\]
Drop the immutable reference before the mutable operation, or restructure the code to use indices instead of references. For example, collect indices to modify first, or clone the referenced data before pushing.
Journey Context:
Developer is iterating over a Vec or holding a reference to an element \(e.g., \`let item = &vec\[i\]\`\), then attempts to mutate the Vec \(e.g., \`vec.push\(new\_item\)\` or \`vec.swap\_remove\(i\)\`\). The compiler throws E0502 because the immutable borrow of \`item\` overlaps with the mutable borrow required by the Vec's \`push\` method. Developer tries to wrap the Vec in \`RefCell\` or \`Mutex\`, which doesn't help because the issue is the fundamental aliasing rule. They search online and find that the borrow checker prevents this to avoid iterator invalidation. The fix works because explicitly dropping the reference \(\`drop\(item\)\`\) before the mutable operation ends the immutable borrow, or using indices \(\`vec\[i\]\`\) instead of references avoids holding a borrow altogether.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T23:15:37.717136+00:00— report_created — created