Report #5604
[bug\_fix] cannot borrow \`self\` as mutable more than once at a time
Restructure to use the Entry API \(e.g., \`map.entry\(key\).or\_insert\(...\)\`\), or drop the first borrow before the second using explicit scopes \(\`\{ let temp = self.map.get\_mut\(...\); \}\`\). Root cause: Rust's aliasing XOR mutation rule prohibits simultaneous mutable references \(\`&mut T\`\) to overlapping data to prevent iterator invalidation and data races.
Journey Context:
You're writing a cache struct with a \`HashMap>\`. In a method \`get\_or\_compute\(&mut self, key: &str\)\`, you try to \`self.map.get\_mut\(key\)\` to check existence. If None, you try \`self.map.insert\(key.to\_string\(\), vec\!\[\]\)\`. The compiler stops you: \`cannot borrow \*self as mutable more than once\`. You try to use \`if let Some\(v\) = ...\` but the borrow lasts until the end of the scope. You realize you need the Entry API: \`self.map.entry\(key.to\_string\(\)\).or\_insert\_with\(Vec::new\)\`. This works because \`entry\(\)\` takes a single mutable borrow at the start, and the resulting Entry handle doesn't conflict with subsequent operations. This teaches that holding \`&mut\` to a collection member blocks all other access to the collection.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T21:44:02.407543+00:00— report_created — created