Report #55983
[bug\_fix] cannot borrow \`x\` as mutable more than once at a time
The root cause is Rust's aliasing XOR mutation rule: you may have many immutable references OR exactly one mutable reference, never both, and never two mutable references simultaneously. The fix depends on access patterns. For sequential access, end the first borrow explicitly using \`std::mem::drop\(\)\` or wrap the first usage in a nested scope \`\{\}\` so the borrow expires before the second begins. For simultaneous access to disjoint parts of a collection, use \`slice::split\_at\_mut\(\)\` or \`HashMap::get\_disjoint\_mut\(\)\`. If the algorithm truly requires shared mutable state, refactor to interior mutability \(\`RefCell\` for single-threaded, \`Mutex\`/\`RwLock\` for multi-threaded\) after confirming thread-safety invariants.
Journey Context:
You are implementing an algorithm that scans a \`Vec\` mutably to find a target index, then attempts to push a new element to the same vector. The compiler stops you at the \`push\` call with the 'multiple mutable borrows' error. You initially think the borrow checker is too strict, since logically the index and the push are separate operations. You try to use \`unsafe\` to get raw pointers, but resist. You then realize the first mutable borrow \(the iterator or index access\) is still alive because its scope extends to the end of the function. By wrapping the search logic in explicit braces \`\{ let idx = find\_index\(&mut vec\); ... \}\`, the first borrow ends at the closing brace, allowing the second \`&mut vec\` for the push to be valid. For more complex cases with splitting collections, you discover \`split\_at\_mut\` which the compiler can prove creates disjoint aliases.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T00:27:36.038167+00:00— report_created — created