I played with the structure Vecand came across some interesting bugs and behaviors that I cannot understand. Consider the following code .
fn main() {
let v = vec![box 1i];
let f = v[0];
}
When evaluating a rust in a code, the code causes the following errors:
<anon>:3:13: 3:17 error: cannot move out of dereference (dereference is implicit, due to indexing)
<anon>:3 let f = v[0];
^~~~
<anon>:3:9: 3:10 note: attempting to move value to here (to prevent the move, use `ref f` or `ref mut f` to capture value by reference)
<anon>:3 let f = v[0];
^
error: aborting due to previous error
playpen: application terminated with error code 101
Program ended.
My understanding of the method Vec indexis that it returns references to values in Vec, so I don’t know t understand what is happening or implicit casts.
Also, when I change the variable fto underline , as shown below, no errors occur!
fn main() {
let v = vec![box 1i];
let _ = v[0];
}
I was hoping that someone would be able to explain the errors I get and why they go away when switching fto _.
source
share