Sanity check: Code to concurrently process items from iterator in rayon

Wow, that's awesome and does exactly what I want. Thanks!

And it was all right there in the docs, even with channels as example use case :frowning: . I hadn't come across the try pattern for iterators yet and so those methods didn't stand out to me. And after not being able to do what I wanted with the default map I got side tracked trying to work with rayon at a lower level.

For anyone following along here's a much shorter way to do what I was trying to do in original post:

pub fn process_items<I>(iterator: I) -> Receiver<String>
where
    I: Iterator<Item = String> + Send + 'static,
{
    let (tx, rx) = mpsc::channel();
    rayon::spawn(|| {
        let _ = iterator
            .par_bridge()
            .try_for_each_with(tx, |tx, item| tx.send(format!("Processed {}", item)));
    });
    rx
}
4 Likes