Report #10277
[bug\_fix] E0502 cannot borrow \`self\` as mutable more than once at a time
Restructure the code to release the first mutable borrow before attempting the second, often by scoping the first borrow in a block or cloning the data needed. Alternatively, use interior mutability patterns \(\`RefCell\`, \`Mutex\`, or \`RwLock\`\) when runtime borrowing checks are acceptable, or redesign the API to avoid simultaneous borrows. Root cause: Rust's borrow checker enforces that mutable references \(&mut T\) must be exclusive \(aliasing XOR mutability\) to prevent data races and iterator invalidation.
Journey Context:
Developer writes a method \`fn process\(&mut self\)\` that iterates over \`self.items\` using an iterator while simultaneously trying to call \`self.modify\_item\(i\)\` inside the loop. The compiler emits E0502: 'cannot borrow \`\*self\` as mutable more than once'. The developer tries \`&mut self\` on the helper method \(same error\), then tries \`&self\` on the helper but it needs to mutate. They consider \`unsafe\` code. After reading the Rust Book chapter on References and Borrowing, they realize they cannot hold the iterator \(which borrows self mutably\) while calling another method that borrows self mutably. They fix it by collecting indices first: \`let keys: Vec<\_> = self.items.keys\(\).collect\(\); for k in keys \{ self.modify\_item\(k\); \}\`. This works because the iterator is dropped before the mutable borrow in \`modify\_item\`. The developer learns the fundamental rule: &mut must be exclusive.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:15:22.144433+00:00— report_created — created