I am trying to pass a modified fragment of a function and use it in several loops inside it.
function1
causes an error. Switching to function2
or function3
leads to the disappearance of errors, but I do not understand the differences between function1
and function2
. v
and &mut *v
seem like me.
Why function1
doesn't it work while others do?
fn main() {
let mut v = Vec::new();
function1(&mut v);
function2(&mut v);
function3(&mut v);
}
fn function1(v: &mut [i32]) {
for l in v {}
for l in v {}
}
fn function2(v: &mut [i32]) {
for l in &mut *v {}
for l in &mut *v {}
}
fn function3(v: &mut [i32]) {
for l in v.iter_mut() {}
for l in v.iter_mut() {}
}
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
source
share