How do I use Rayon with an existing iterator?

旧巷老猫 提交于 2019-12-01 17:13:58

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<i32>`
    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<Item> and a next() method. You cannot split it in two parts to execute them in different threads.

Jesse Grosjean

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!"]);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!