This will fail:
fn ppv(arr: &mut Vec<i32>) {
if arr.len() <= 0 {
return;
}
let mut pp: i32 = 0;
for &mut val in arr {
if val == pp {
pp = val;
}
}
println!("arr is {:?}", &arr);
}
But this will pass:
fn ppv(arr: &mut Vec<i32>) {
if arr.len() <= 0{
return;
}
let mut pp: i32 = 0;
for &mut val in arr.into_iter() {
if val == pp {
pp = val;
}
}
println!("arr is {:?}", &arr);
}
when I compile the first one, it failed:
error[E0382]: borrow of moved value: `arr`
--> src/main.rs:12:29
|
2 | fn ppv(arr: &mut Vec<i32>) {
| --- move occurs because `arr` has type `&mut Vec<i32>`, which does not implement the `Copy` trait
...
7 | for &mut val in arr {
| --- `arr` moved due to this implicit call to `.into_iter()`
...
12 | println!("arr is {:?}", &arr);
| ^^^^ value borrowed here after move
|
Why is that? Is it interpreting it differently?
First case will implicit call into_iter()
, it failed, when I call into_iter()
, it passed. What happened?