error[E0499]: cannot borrow `x` as mutable more than once at a time
すでに可変参照されている領域を可変参照しようとした。
(可変参照は排他参照とも呼ばれる。)
すでに可変参照されている変数を可変参照している。
fn main() {
let mut value = 0;
let mut mr1 = &mut value;
let mut mr2 = &mut value;
println!("{mr1}");
println!("{mr2}");
}
error[E0499]: cannot borrow `value` as mutable more than once at a time --> src/main.rs:4:19 | 3 | let mut mr1 = &mut value; | ---------- first mutable borrow occurs here 4 | let mut mr2 = &mut value; | ^^^^^^^^^^ second mutable borrow occurs here 5 | println!("{mr1}"); | ----- first borrow later used here
過去のループで保存した可変参照と競合している。
fn main() {
let mut value = 0;
let mut refs = vec![];
for _ in 0..2 {
refs.push(&mut value);
}
}
error[E0499]: cannot borrow `value` as mutable more than once at a time --> src\main.rs:5:19 | 5 | refs.push(&mut value); | ---- ^^^^^^^^^^ `value` was mutably borrowed here in the previous iteration of the loop | | | first borrow used here, in later iteration of loop
詳しくは『スライスの分割と split_at_mut 系の関数』を参照。
fn main() {
let mut obj = [0, 0];
let a = &mut obj[0];
let b = &mut obj[1];
*a += 1;
*b += 1;
assert_eq!(obj[0], obj[1]);
}
error[E0499]: cannot borrow `obj[_]` as mutable more than once at a time --> src\main.rs:4:13 | 3 | let a = &mut obj[0]; | ----------- first mutable borrow occurs here 4 | let b = &mut obj[1]; | ^^^^^^^^^^^ second mutable borrow occurs here 5 | *a += 1; | ------- first borrow later used here | = help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices
2025 年現在の借用チェッカー NLL の偽陽性が原因になっている。
詳しくは『借用チェッカーの制限 - 偽陽性の例 2』を参照。
GAT を使っている場合、それによる擬陽性もよく発生する。
詳しくは『E0499 - NLL の偽陽性 × GAT』を参照。