Report #78043
[bug\_fix] cannot borrow \`x\` as mutable more than once at a time
Restructure the code to ensure that mutable borrows do not overlap in scope. This can be done by using nested blocks to limit the lifetime of each borrow, splitting the data structure using methods like \`split\_at\_mut\` for slices, or collecting results into a temporary collection before mutating the original.
Journey Context:
The developer is iterating over a vector and trying to push new elements into it within the same loop, or they are holding a mutable reference to a field while trying to call another method on the same struct that also requires \`&mut self\`. The compiler points to the second borrow site with a long note explaining that the first borrow lasts until the end of the scope. The developer initially tries to clone the data to avoid the borrow, but this is inefficient or impossible for some types. They then consider using \`unsafe\` or \`RefCell\`, but realize this is overkill and potentially unsound. After examining the control flow, they realize they can either perform all the read-only operations first, collecting the necessary data into a local variable, then drop any borrows before starting the mutable phase; or they can use \`Vec::split\_at\_mut\` to create two disjoint mutable slices that the borrow checker can prove do not alias. This restructuring resolves the conflict by making the non-overlapping nature of the borrows explicit to the compiler.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T13:35:46.395350+00:00— report_created — created