Report #93022
[bug\_fix] error\[E0502\]: cannot borrow \`vec\` as mutable because it is also borrowed as immutable
Restructure the code to ensure the immutable borrow ends before the mutable borrow begins, typically by wrapping the immutable usage in a nested block scope or cloning the immutable data needed before mutating.
Journey Context:
Developer is iterating over a collection with \`.iter\(\)\` \(immutable borrow\) while inside the loop trying to call \`.push\(\)\` or \`.clear\(\)\` \(mutable borrow\) on the same collection. The compiler points to the mutable borrow site. Developer first tries to clone the collection at the start, but this is expensive. They then try to collect the iterator into a Vec first, but this consumes the iterator. After reading the error explanation, they realize the immutable borrow from \`.iter\(\)\` is held for the entire loop body. The fix is to either collect the keys/indices needed first into a separate vector \(owned\), or restructure to perform all immutable work in a nested block so the borrow ends before the mutable operation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T14:43:32.059734+00:00— report_created — created