While I agree that internal iteration is the easiest way to do this, something like OpenMP for C/C++ does do parallelisation of (simplistic) external iterator based for loops, so it is possible.
That said, I think trying to reuse the for construct directly is not necessarily the right way to do this; Iād prefer making iterator consumers that take closures to run in parallel, e.g. at the most basic, we could have a function
impl ThreadPool {
/// Execute `f` on each element of `it`, in parallel
fn par_for<It, F>(&self, it: It, f: F)
where It::Item: Sync + Send,
F: Fn(It::Item) + Sync {
// ...
}
}
used like pool.par_for(some_map.iter(), |&(k, v)| { ... }) or pool.par_for(some_slice.chunks(10), |elems| { ... }) (run on 10 elements at a time) etc.
This approach is more DRY than having each type yield a hard-coded concurrent iterator, and thereās no particular reason we canāt define parallel versions of the adaptors like pool.par_map that returns an Iterator so that, e.g.
let vec = pool.par_map(some_iter, |elem| { ... }).collect();
works, where the closure is run in parallel across some_iter.
(That said, internal iteration does mean that a type can more easily reuse internal structure to divide work out.)