I want to get a function to accept two Iterators and a callback and process as follows:
fn for_xy<I: Iterator<Item = usize>, F>(ix: I, iy: I, f: F)
where
I: Iterator<Item = usize>,
F: FnMut(usize, usize) -> (),
{
for x in ix {
for y in iy {
f(x, y);
}
}
}
but I get this error:
error[E0382]: use of moved value: `iy`
--> src/lib.rs:7:18
|
1 | fn for_xy<I: Iterator<Item = usize>, F>(ix: I, iy: I, f: F)
| -- move occurs because `iy` has type `I`, which does not implement the `Copy` trait
...
7 | for y in iy {
| ^^ `iy` moved due to this implicit call to `.into_iter()`, in previous iteration of loop
|
note: this function takes ownership of the receiver `self`, which moves `iy`
help: consider borrowing to avoid moving into the for loop
|
7 | for y in &iy {
| ^^^
help: consider further restricting this bound
|
3 | I: Iterator<Item = usize> + Copy,
| ^^^^^^
How can I correct this program?