Report #57233
[bug\_fix] cannot borrow \`self\` as mutable more than once at a time \(E0499\)
Restructure the code to avoid holding a mutable reference across conflicting scopes. Use temporary scopes, split mutable borrows by distinct fields \(struct splitting\), or collect indices/keys into a Vec first and process in a second loop. The root cause is Rust's aliasing XOR mutation rule: overlapping lifetimes of mutable references to the same data are forbidden to prevent data races and iterator invalidation.
Journey Context:
You write a method on a struct that iterates with \`for item in &mut self.items\` and inside the loop calls \`self.process\(item\)\` which also takes \`&mut self\`. The compiler stops you with E0499. You try to use RefCell<>, but that panics at runtime. You search 'rust cannot borrow self as mutable more than once' and find explanations about splitting borrows. You realize you're holding the iterator's mutable borrow across the method call. You refactor to first collect the items needing processing into a Vec, drop the first borrow, then process them in a second loop. This satisfies the borrow checker because the borrows no longer overlap.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T02:33:03.385908+00:00— report_created — created