初級 › E0596: cannot borrow as mutable の原因と直し方 › パターン1
E0596: cannot borrow as mutable の原因と直し方
mutを付けずに宣言した変数を、&mutで可変参照として渡そうとするとE0596エラーになります。
1 fn increment(n: &mut i32) { 2 *n += 1; 3 } 4 5 fn main() { 6 let ______x = 5; ^ 7 increment(&mut x); 8 println!("{}", x); 9 }
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
空欄を埋めて実行を通す
&mutで可変参照として渡すには、渡す側の変数自体がmutで宣言されている必要があります。
次の問題