中級 › E0502: cannot borrow as mutable because it is also borrowed as immutable › パターン3
E0502: cannot borrow as mutable because it is also borrowed as immutable
不変参照が生きている(後で使われる)間に、同じ変数を可変で借用しようとするとE0502エラーになります。
1 fn main() { 2 let mut names = vec![String::from("a")]; 3 let first_name = &names[0]; 4 ______________________________ ^ 5 println!("{}", first_name); 6 }
error[E0502]: cannot borrow `names` as mutable because it is also borrowed as immutable
空欄を埋めて実行を通す
不変参照と可変参照は同時に存在できません。参照を使い終わってから変更する必要があります。
次の問題