中級 › E0502: cannot borrow as mutable because it is also borrowed as immutable › パターン5
E0502: cannot borrow as mutable because it is also borrowed as immutable
不変参照が生きている(後で使われる)間に、同じ変数を可変で借用しようとするとE0502エラーになります。
1 fn main() { 2 let mut scores = vec![70, 80]; 3 let top = &scores[1]; 4 _________________ ^ 5 println!("{}", top); 6 }
error[E0502]: cannot borrow `scores` as mutable because it is also borrowed as immutable
空欄を埋めて実行を通す
Vecへの追加は内部でメモリの再確保が起きうるため、既存の参照との共存を許すと危険です。
次の問題