中級 › E0502: cannot borrow as mutable because it is also borrowed as immutable › パターン4
E0502: cannot borrow as mutable because it is also borrowed as immutable
不変参照が生きている(後で使われる)間に、同じ変数を可変で借用しようとするとE0502エラーになります。
1 fn main() { 2 let mut prices = vec![100, 200]; 3 let cheapest = &prices[0]; 4 ________________ ^ 5 println!("{}", cheapest); 6 }
error[E0502]: cannot borrow `prices` as mutable because it is also borrowed as immutable
空欄を埋めて実行を通す
参照が有効な間は元のVecへの変更操作(追加や並べ替え)は一切できません。
次の問題