I have the below Rust program.
fn main() {
let v = vec![100, 32, 57];
for i in v {
println!("{}", i);
}
println!("{:?}", v);
}
When I run it, I get:
error[E0382]: borrow of moved value: `v`
--> src\main.rs:7:22
|
2 | let v = vec![100, 32, 57];
| - move occurs because `v` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
3 | for i in v {
| -
| |
| value moved here
| help: consider borrowing to avoid moving into the for loop: `&v`
...
7 | println!("{:?}", v);
| ^ value borrowed here after move
The error states that there is a move happened at for i in v
. But I'm just using the same variable v
defined by let v = vec![100, 32, 57]
. It's not something like let v2 = v; for i in v2 ...
, which moved the value from v
to v2
. Could anyone help to explain a little bit?