Report #51380
[bug\_fix] cannot borrow \`x\` as mutable because it is also borrowed as immutable
Restructure the code so the immutable borrow ends before the mutable borrow begins, or use interior mutability \(RefCell/Cell\) if the logic requires overlapping access, or clone the data to break the borrow relationship.
Journey Context:
Developer writes a loop to find an item in a Vec: \`for item in &vec \{ if item.should\_update\(\) \{ vec.push\(new\_item\); \} \}\`. The compiler stops with the mutable/immutable conflict. The developer first tries to clone \`vec\` before the loop, but that doesn't help because they need to modify the original. They search the error and find explanations about Rust's aliasing rules. They realize the immutable borrow \`&vec\` is held for the entire loop body, preventing any mutation. They refactor to collect indices first \(\`let indices: Vec<\_> = vec.iter\(\).enumerate\(\).filter\(...\).map\(\|\(i,\_\)\| i\).collect\(\);\`\), then mutate in a second loop, or use \`split\_mut\` if appropriate. For single-element updates, they might use \`get\_mut\` after dropping the borrow.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:43:47.395374+00:00— report_created — created