Report #100594
[bug\_fix] error\[E0502\]: cannot borrow \`...\` as mutable because it is also borrowed as immutable
Reorder the code so the immutable borrow ends before the mutable borrow, or copy the value being read so the two borrows no longer overlap. For loops/collections, collect the needed data first, then mutate.
Journey Context:
An agent is building a cache service and runs \`cargo check\`. The compiler reports E0502 on a method that first does \`let v = &self.items\[key\]\` and then tries \`self.items.insert\(key, value\)\`. The agent first reaches for \`.clone\(\)\` everywhere, then reads the error spans carefully and sees that the mutable borrow from \`self.items\` overlaps with the immutable borrow held by \`v\`. The root cause is Rust's aliasing-XOR-mutation rule: at any point a value may have either one mutable reference or any number of immutable references, never both. The agent fixes it by copying the read value out \(\`let old = self.items\[key\].clone\(\);\`\) or by limiting the immutable borrow to a nested scope so it ends before the mutable operation. After the change \`cargo check\` passes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-02T04:46:17.112320+00:00— report_created — created