S0131: cannot borrow as mutable because value is already borrowed
Generated from rellum-diagnostics/src/db.rs.
Explanation
A mutable reference requires exclusive access to its referent. If a shared borrow is still live, creating &mut would allow mutation while another reader observes the same value.
Common causes
- creating &mut while an & reference to the same value is still used
- passing a value to a borrowing API and then mutably borrowing it in the same live region
- keeping a shared reference in a local binding across a later mutable borrow
Canonical fixes
- end the shared borrow before creating the mutable borrow
Before:
r = &buf
m = &mut buf
use_both(r, m)
After:
r = &buf
n = len(r)
m = &mut buf
- move the mutable operation before shared borrows that need the old value
Code example
r = &buf
m = &mut buf
use_both(r, m)