Report #7045
[bug\_fix] cannot borrow \`vec\` as mutable more than once at a time
Restructure to avoid simultaneous borrows: collect modification targets into a temporary Vec first, then apply changes, or use slice patterns like \`split\_first\_mut\` to create disjoint borrows instead of indexing.
Journey Context:
Developer writes a game loop using \`for i in 0..vec.len\(\)\`, reading \`vec\[i\]\` to check a condition, then inside the same scope calling \`vec.push\(\)\` or \`vec.swap\(\)\`. The compiler rejects it because indexing \`vec\[i\]\` creates a borrow of the whole vector that overlaps with the mutable borrow from \`push\`. Developer tries wrapping the Vec in \`RefCell\`, getting runtime panics instead. They search and realize the borrow checker sees overlapping lifetimes for the entire container during indexed access. The fix is to collect indices to modify first into a temporary Vec, then loop over that to mutate, or use \`split\_first\_mut\` to disjointly borrow head and tail, avoiding indexed borrowing in the same scope as mutation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:41:39.269664+00:00— report_created — created