I'm trying to pass a mutable slice to a function and use it in several loops inside it.
function1 produces an error. Changing to function2 or function3 makes the errors disappear, but I don't understand the differences between function1 and function2. v and &mut *v seem similar to me.
Why doesn't function1 work while the others do?
fn main() {
let mut v = Vec::new();
function1(&mut v);
function2(&mut v);
function3(&mut v);
}
// Move Error
fn function1(v: &mut [i32]) {
for l in v {}
for l in v {} // <-- Error Here !!!
}
// Works Fine
fn function2(v: &mut [i32]) {
for l in &mut *v {}
for l in &mut *v {}
}
// Works Fine
fn function3(v: &mut [i32]) {
for l in v.iter_mut() {}
for l in v.iter_mut() {}
}
The error:
error[E0382]: use of moved value: `v`
--> src/main.rs:12:14
|
11 | for l in v {}
| - value moved here
12 | for l in v {} // <-- Error Here !!!
| ^ value used here after move
|
= note: move occurs because `v` has type `&mut [i32]`, which does not implement the `Copy` trait