Report #57971
[bug\_fix] cannot borrow \`self\` as mutable more than once at a time \(E0499\) when modifying a HashMap or struct field while a previous mutable borrow is active, such as checking \`contains\_key\` then trying to \`insert\`.
Use the \`Entry\` API \(e.g., \`map.entry\(key\).or\_default\(\).push\(val\)\`\) to check and insert in one operation, or collect keys into a \`Vec\` first to break the borrow chain. Root cause: Rust's aliasing XOR mutation guarantee prevents holding multiple mutable references \(\`&mut T\`\) simultaneously to prevent data races and invalidation.
Journey Context:
You are implementing an in-memory rate limiter with a \`HashMap>\`. In \`fn check\(&mut self, key: &str\)\`, you write \`if self.requests.contains\_key\(key\) \{ self.requests.get\_mut\(key\).unwrap\(\).push\(now\); \} else \{ self.requests.insert\(key.to\_string\(\), vec\!\[now\]\); \}\`. The compiler errors on the \`insert\` call because \`contains\_key\` held an implicit \`&mut self\` borrow. You try splitting into two methods, but the borrow checker sees the same lifetime. You discover the \`HashMap::entry\` API, allowing \`self.requests.entry\(key.to\_string\(\)\).or\_default\(\).push\(now\);\`, which acquires one mutable borrow for the entire operation, satisfying the borrow checker.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T03:47:47.170747+00:00— report_created — created