I want to store a batch of data somewhere and refer to them from multiple spaces, and I have serious trouble with borrowing:
fn main() {
let mut v1: Vec<i32> = Vec::new();
let mut v2: Vec<&i32> = Vec::new();
for i in 1..10 {
v1.push(i);
v2.push(v1.last().unwrap());
}
println!("{:?}", v1);
println!("{:?}", v2);
}
error[E0502]: cannot borrow `v1` as mutable because it is also borrowed as immutable
--> src/main.rs:6:9
|
6 | v1.push(i);
| ^^ mutable borrow occurs here
7 | v2.push(v1.last().unwrap());
| -- immutable borrow occurs here
...
11 | }
| - immutable borrow ends here