How do I use Rayon with an existing iterator?

前端 未结 2 1214
独厮守ぢ
独厮守ぢ 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:12

    This answer is outdated for the last version of rayon. See the other answer for a possible solution. It may or may not apply to your usecase.


    Minimal reproduction:

    extern crate rayon;
    
    use rayon::prelude::*;
    
    fn main() {
        let v = vec![1_i32, 2, 3, 4].into_iter();
    
        // no method named `par_iter` found for type `std::vec::IntoIter`
        let _ = v.par_iter().sum();
    }
    

    You cannot do that. Here are all the implementors of this feature, that are:

    • BinaryHeap
    • BTreeMap
    • BTreeSet
    • HashMap
    • HashSet
    • LinkedList
    • VecDeque
    • Option
    • Range
    • Result
    • Slice/Array

    I think that the reason why you cannot parallelize them is because iterators are lazy. An iterator is basically a current item Option and a next() method. You cannot split it in two parts to execute them in different threads.

提交回复
热议问题