For future reference, it’s extremely useful if you include the compiler error message (below) and/or a playground link of the code that’s causing you problems.
error[E0382]: use of moved value: `v1_iter`
--> src/main.rs:9:22
|
3 | let v1_iter = v1.iter();
| ------- move occurs because `v1_iter` has type `std::slice::Iter<'_, i32>`, which does not implement the `Copy` trait
4 |
5 | for val in v1_iter {
| -------
| |
| `v1_iter` moved due to this implicit call to `.into_iter()`
| help: consider borrowing to avoid moving into the for loop: `&v1_iter`
...
9 | let total: i32 = v1_iter.sum();
| ^^^^^^^ value used here after move
|
note: this function consumes the receiver `self` by taking ownership of it, which moves `v1_iter`
Iterators in Rust are one-shot: they will produce each value once and never again. Because if this, for
loops take ownership of the iterator they’re walking— by the time the loop exits, the iterator is exhausted.
You’ll need to make a new iterator for the sum call:
let total: i32 = v1.iter().sum();