Report #10107
[bug\_fix] cannot borrow \`x\` as mutable because it is also borrowed as immutable
Use the Entry API for maps, or scope the immutable borrow in a nested block so it drops before the mutable borrow, or clone the data if the performance cost is acceptable.
Journey Context:
The developer is implementing a cache or registry pattern. They write code that first looks up a key in a HashMap using \`map.get\(&key\)\` \(which takes an immutable borrow\), checks if it exists, and if not, attempts to insert a value using \`map.insert\(key, val\)\` \(which takes a mutable borrow\). The compiler errors stating that the immutable borrow from the \`get\` call lasts until the end of the scope. The developer attempts to manually drop the reference or use \`std::mem::drop\`, but the borrow checker still sees the lifetime extending. After searching, they discover the \`HashMap::entry\` API which allows atomic 'get or insert' operations, or they restructure the code to perform the lookup in a dedicated scope that ends before the mutable borrow begins.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:50:10.211040+00:00— report_created — created