let mut u:[usize;3] = [0;3];
let mut v = vec![];
for i in 0..3 {
u[i] = i;
let e = &(u[i]);
v.push(e);
}
error[E0506]: cannot assign to `u[_]` because it is borrowed
--> src/lib.rs:5:9
|
5 | u[i] = i;
| ^^^^^^^^ assignment to borrowed `u[_]` occurs here
6 | let e = &(u[i]);
| ------- borrow of `u[_]` occurs here
7 | v.push(e);
| - borrow later used here
I understand the compiler is saving me from myself, but I really want this behavior, and I don't want to force it through with unsafe as I'm sure to screw up. What are my options?
I have a feeling it has something to do with lifetimes which I have yet to comprehend. Perhaps it's something to do with having two owners for the same data? I wouldn't think this had to be an issue as long as u outlived v.
How can I recover from this? Or is it just plain disallowed due to multiple owners and thus I need to rethink my approach?
I'm going through this trouble because I need to copy a type that only can be cloned but I'll have to ask another question for that.