初級 › E0596: cannot borrow as mutable の原因と直し方 › パターン3
E0596: cannot borrow as mutable の原因と直し方
mutを付けずに宣言した変数を、&mutで可変参照として渡そうとするとE0596エラーになります。
1 fn add_bonus(n: &mut i32) { 2 *n += 10; 3 } 4 5 fn main() { 6 let ______score = 80; ^ 7 add_bonus(&mut score); 8 println!("{}", score); 9 }
error[E0596]: cannot borrow `score` as mutable, as it is not declared as mutable
空欄を埋めて実行を通す
可変で借用できるかどうかは、あくまで元の変数の宣言時の状態で決まります。
次の問題