Is there a way to directly consume a Rayon chain without collecting it first?

…衆ロ難τιáo~ 提交于 2019-12-24 00:40:02

问题


I am using Rayon to produce reasonably large return values. This uses a lot of memory when collecting all returned values into a Vec. Is there a way to avoid creating a Vec and directly consuming as an iterable?

Here is an example which doesn't work:

fn main() {
    let numbers: Vec<_> = "12.03 0.3 44.2 45 zzz".split_whitespace().collect();

    let x = numbers
        .par_iter()
        .map(|n| n.parse::<f32>())
        .filter_map(|n| n.ok());

    for n in x {
        println!("{:?}", n);
    }
}
error[E0277]: the trait bound `rayon::iter::FilterMap<rayon::iter::Map<rayon::slice::Iter<'_, &str>, [closure@src/main.rs:10:14: 10:34]>, [closure@src/main.rs:11:21: 11:31]>: std::iter::Iterator` is not satisfied
   |
13 |     for n in x {
   |              ^ `rayon::iter::FilterMap<rayon::iter::Map<rayon::slice::Iter<'_, &str>, [closure@src/main.rs:10:14: 10:34]>, [closure@src/main.rs:11:21: 11:31]>` is not an iterator; maybe try calling `.iter()` or a similar method
   |
   = help: the trait `std::iter::Iterator` is not implemented for `rayon::iter::FilterMap<rayon::iter::Map<rayon::slice::Iter<'_, &str>, [closure@src/main.rs:10:14: 10:34]>, [closure@src/main.rs:11:21: 11:31]>`
   = note: required by `std::iter::IntoIterator::into_iter`

playground


回答1:


You are looking for ParallelIterator::for_each:

extern crate rayon; // 1.0.2

use rayon::prelude::*;

fn main() {
    let numbers = ["12.03", "0.3", "44.2", "45", "zzz"];

    let x = numbers.par_iter().flat_map(|n| n.parse::<f32>());

    x.for_each(|n| {
        println!("{:?}", n);
    });
}

Related:

  • ParallelIterator::for_each_with
  • ParallelIterator::try_for_each
  • ParallelIterator::try_for_each_with

See also:

  • How to satisfy the Iterator trait bound in order to use Rayon here?


来源:https://stackoverflow.com/questions/51465467/is-there-a-way-to-directly-consume-a-rayon-chain-without-collecting-it-first

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