不変変数
2022/2/16 23:49:00
ベクタの要素を不変変数に借用した場合は、そのベクタの要素を変更・追加できなくなる
let mut v = vec![1, 2, 3, 4, 5];
let first = &v[0];
v.push(6);
println!("The first element is: {}", first);
/*
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
--> src\main.rs:23:5
|
21 | let first = &v[0];
| - immutable borrow occurs here
22 |
23 | v.push(6);
| ^^^^^^^^^ mutable borrow occurs here
24 |
25 | println!("The first element is: {}", first);
| ----- immutable borrow later used here
*/