我有以下 Rust 程序。
fn main() {
let v = vec![100, 32, 57];
for i in v {
println!("{}", i);
}
println!("{:?}", v);
}
运行时我得到了:
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
错误说明在
for i in v
发生了一个move,但我只是在使用由let v = vec![100, 32, 57]
定义的同一变量v
。这不像是let v2 = v; for i in v2 ...
那样,将值从v
移动到v2
。能否有人帮忙解释一下?