Report #14500
[bug\_fix] cannot borrow \`x\` as mutable more than once at a time — second mutable borrow occurs here
Restructure the code to separate the read and write phases. Collect the keys or indices to modify into a temporary vector during an immutable iteration, then iterate over the temporary vector to perform mutations, ensuring no overlapping borrows exist simultaneously. Alternatively, use \`split\_mut\` or interior mutability patterns like \`RefCell\` if single-owner rules are too restrictive.
Journey Context:
You are implementing a game loop with a \`HashMap\` representing entities. During the update phase, you iterate with \`for \(id, player\) in &mut players\` and attempt to look up another player using \`players.get\_mut\(&target\_id\)\` to apply damage. The compiler halts with "cannot borrow \`players\` as mutable more than once." You attempt to use raw pointers or \`unsafe\` blocks, realizing that's unsound. You try wrapping the HashMap in \`RefCell\`, but get a runtime borrow panic because the mutable iterator already holds a borrow. You realize the fundamental rule: you cannot have a mutable reference to the collection while iterating over it, because the iterator itself holds a borrow. You refactor to first collect all the \`\(id, target\_id, damage\)\` tuples into a \`Vec\` using an immutable iteration \(or \`retain\`\), then iterate over that \`Vec\` to perform the \`players.get\_mut\(\)\` lookups, effectively decoupling the read phase from the write phase and satisfying the borrow checker.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:44:39.352869+00:00— report_created — created