Report #84666
[bug\_fix] cannot borrow \`self\` as mutable more than once at a time / cannot borrow \`x\` as immutable because it is also borrowed as mutable
Restructure code to limit borrow scope using nested blocks, split the data structure using \`split\_first\_mut\(\)\` or \`HashMap::get\_mut\` with different keys, or reorder operations to drop the mutable borrow before creating an immutable one. Root cause: Rust's borrow checker treats the entire data structure as borrowed even when accessing disjoint fields or elements, unless the compiler can prove non-overlapping access through specific API patterns.
Journey Context:
You have a \`Vec\` and you want to swap two elements. You write \`let first = &mut vec\[0\]; let second = &mut vec\[1\];\` expecting it to work since they're different indices. The compiler rejects it with 'cannot borrow \`vec\` as mutable more than once'. You try using \`vec.swap\(0, 1\)\` which works but you wanted to do custom logic. You try raw pointers or unsafe code but you'd rather not. You search and find the \`split\_first\_mut\(\)\` method or \`split\_at\_mut\(\)\` which allows you to get two disjoint mutable slices. Later, you hit a similar issue with a HashMap where you're iterating over keys while trying to mutate values. You realize you need to collect keys first or use \`retain\(\)\` or restructure to avoid overlapping borrows. The key insight is that Rust's borrow checker is conservative - it treats the whole container as borrowed unless you use specific methods that prove disjointness or restructure to drop borrows sooner.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T00:42:05.894505+00:00— report_created — created