How do I use Rayon with an existing iterator?

前端 未结 2 1189
独厮守ぢ
独厮守ぢ 2021-01-18 03:32

I turn a regex into a HashSet after doing some filtering. I am trying to use it with Rayon, but I can\'t figure out how to make Rayon work with an existing iter

2条回答
  •  再見小時候
    2021-01-18 04:16

    This is possible now with ParallelBridge:

    use rayon::iter::ParallelBridge;
    use rayon::prelude::ParallelIterator;
    use std::sync::mpsc::channel;
    
    let rx = {
        let (tx, rx) = channel();
    
        tx.send("one!");
        tx.send("two!");
        tx.send("three!");
    
        rx
    };
    
    let mut output: Vec<&'static str> = rx.into_iter().par_bridge().collect();
    output.sort_unstable();
    
    assert_eq!(&*output, &["one!", "three!", "two!"]);
    

提交回复
热议问题