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?
iymany times, once for each item inix. I suggest accepting slices instead of iterators, since they can be iterated over many timesIterator<item = &usize>and useiy.copied().Clone- many iterators (including those that iterate overVecs) are.Clone(and iterating overiy.clone()) is better thanCopy, because more types implementClone. One such type isstd::ops::Rangewhich seems a likely candidate to be passed to a function like this.