Report #101483
[bug\_fix] cannot borrow \`map\` as mutable more than once at a time while iterating and conditionally inserting into a HashMap
Use the Entry API \(\`map.entry\(key\).and\_modify\(...\).or\_insert\(...\)\`\) so the map is borrowed mutably exactly once per key, instead of holding a \`&mut V\` from \`get\_mut\` while also calling \`insert\`.
Journey Context:
You write a loop that walks a list of keys, looks each up with \`map.get\_mut\(&k\)\`, and if it is missing calls \`map.insert\(k, v\)\`. The compiler stops with a double mutable borrow because \`get\_mut\` returns a \`&mut V\` tied to the whole map for the rest of the scope, and \`insert\` needs another \`&mut self\` on the same map. You try narrowing scopes with extra braces and temporary variables, but the borrow is still live. The Entry API was designed for this exact case: \`entry\` gives you a single mutable view \(\`OccupiedEntry\` or \`VacantEntry\`\) that can either update an existing value or insert a new one without ever creating overlapping borrows. Rewriting with \`.entry\(k\).or\_insert\(v\)\` satisfies the borrow checker and removes the need for the lookup-plus-insert dance.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-07T04:56:03.321293+00:00— report_created — created