I am using rayon::par_iter, and I have an
Normally, I can do
x.par_iter().map(...).collect()which processes all elements. However, I only want to process the first
nelements (without truncating
xitself to n elements).
nelements, is there a way to
par_iterover only the first
It can run on a slice too;
Yes, a slice will do this. FWIW, I think
x[..n] is more idiomatic, but there’s no semantic difference.
You can also
.take(n) on any
IndexedParallelIterator. This might be preferable if you’re not sure that your source actually has at least
n elements, otherwise slicing will panic out of bounds.
If so, the ‘laziness’ of iters always confuses me, as intuitively, I imagine a ‘fork’ happening at the
par_iter() step, and thus the
take(n) makes no sense. However, given we’re just “building up up a computation graph”, and executing it later at
collect(), then this makes sense.
iter should be renamed
Yes, and you’re right that laziness means nothing starts on the threadpool here yet.