初級 › E0596: cannot borrow as mutable の原因と直し方 › パターン5
E0596: cannot borrow as mutable の原因と直し方
mutを付けずに宣言した変数を、&mutで可変参照として渡そうとするとE0596エラーになります。
1 fn halve(n: &mut i32) { 2 *n /= 2; 3 } 4 5 fn main() { 6 let ______amount = 40; ^ 7 halve(&mut amount); 8 println!("{}", amount); 9 }
error[E0596]: cannot borrow `amount` as mutable, as it is not declared as mutable
空欄を埋めて実行を通す
mutを付け忘れると、値を変更する関数に渡す時点でコンパイルエラーになります。
次の問題