I have a vector of vector like the following.
let m: Vec<Vec<u64>> = vec![
vec![1, 2, 3, 4, 5],
vec![1, 2, 3, 4, 5],
vec![1, 2, 3, 4, 5],
vec![1, 2, 3, 4, 5],
vec![1, 2, 3, 4, 5]
];
And add function that is intended add each element of two vector.
fn add(xs: &Vec<u64>, ys: &Vec<u64>) -> Vec<u64> {
xs.iter().zip(ys.iter()).map(|(x, y)| x + y).collect()
}
Now, I want to fold the vector of vector m.
What I tried is:
let s: Vec<u64> = m[1..]
.iter()
.fold(m[0], |acc, xs| add(&acc, xs));
But this code doesn't pass the compiler.
|
16 | .fold(m[0], |acc, xs| add(&acc, xs));
| ^^^^ move occurs because value has type `Vec<u64>`, which does not implement the `Copy` trait
I tried prepend & but the compiler still rejects:
|
16 | .fold(&m[0], |acc, xs| add(&acc, xs));
| ^^^^^ expected struct `Vec`, found `&Vec<u64>`
|
= note: expected struct `Vec<u64>`
found reference `&Vec<u64>`
help: consider removing the borrow
|
16 - .fold(&m[0], |acc, xs| add(&acc, xs));
16 + .fold(m[0], |acc, xs| add(&acc, xs));
|
I think the signature of add function is correct as it doesn't want to take ownership of the arguments vectors, and it returns a new vector so the ownership should be passed to the caller.
Actually, I tried every possible combinations (add/remove & from the function, etc.), but couldn't make the code pass the compiler.
Can you let me know what I missed, what is wrong with the above code? Thanks.