Report #7404
[bug\_fix] cannot borrow \`data\` as mutable more than once at a time
Restructure to avoid simultaneous iteration and mutation by collecting keys to remove into a temporary Vec first, or use \`HashMap::retain\` which provides mutable access safely during iteration. Root cause: Rust's borrow checker enforces that only one mutable reference to data can exist at any time to prevent data races and iterator invalidation.
Journey Context:
Developer is implementing a connection manager in a async web server. They write a cleanup routine that iterates over a \`HashMap\` called \`active\_users\`. Inside the loop, they check \`if conn.is\_disconnected\(\)\` and immediately call \`active\_users.remove\(&id\)\`. The compiler throws the double mutable borrow error. Developer first tries to wrap the HashMap in \`RefCell\`, which fails because \`RefCell\` is not Sync. They then try to clone the entire map, which kills performance. After reading the error message carefully, they realize they can use \`let keys: Vec<\_> = active\_users.keys\(\).cloned\(\).collect\(\);\` first, then iterate \`keys\` and call \`remove\` inside, satisfying the borrow checker because the iterator is no longer holding a reference to the map during mutation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:40:00.315669+00:00— report_created — created