Report #96824
[bug\_fix] E0499: cannot borrow \`x\` as mutable more than once at a time in nested closures
Replace the direct HashMap with \`RefCell>\` to enable interior mutability, or restructure to avoid simultaneous borrows. Root cause: Rust's aliasing rules forbid multiple mutable references \(\`&mut T\`\) to the same data from overlapping scopes, even in different closures.
Journey Context:
Developer creates a \`HashMap\` and tries to call \`get\_mut\` on two different keys simultaneously inside a \`match\` or closure to swap values. The compiler stops at the second \`get\_mut\` with E0499. Developer tries to use raw pointers \(\`as\_mut\_ptr\(\)\`\), which is unsafe and error-prone. They consider using \`unsafe\` blocks but realize the borrow checker is protecting against use-after-free if the map reallocates. They discover \`RefCell\` from the \`std::cell\` module, wrap the HashMap in it, and use \`borrow\_mut\(\)\` to get a mutable reference. This shifts borrow checking from compile-time to runtime, panicking if rules are violated \(which they avoid by careful ordering\). The fix works because \`RefCell\` uses dynamic checks to enforce the single mutable reference rule at runtime, allowing the code patterns that the static checker rejects but which are actually safe.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T21:06:14.539136+00:00— report_created — created