Report #52865
[bug\_fix] cannot borrow \`self\` as mutable because it is also borrowed as immutable \(E0502\)
Restructure the code to ensure the immutable borrow ends before the mutable borrow begins, typically by scoping the immutable reference into a smaller block or cloning the data needed before mutating.
Journey Context:
You are implementing a method on a struct holding a \`Vec\`. You write \`for item in &self.items \{ self.mutate\_something\(\); \}\` and the compiler throws E0502. You stare at the error, confused because you think "I'm only reading in the loop." You try to wrap \`self\` in \`RefCell\`, but that feels wrong. You realize the immutable borrow \`&self\` in the \`for\` loop lasts for the entire loop body, overlapping with the \`&mut self\` required by \`mutate\_something\`. You fix it by collecting the items you need into a local \`Vec\` before the loop, or by scoping the immutable borrow so it drops before the mutation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T19:13:44.545570+00:00— report_created — created