我尝试在Rust中逐行处理文件,并使用Rayon将其并行化。它会抱怨以下几点
rayon::str::Lines<'_>` is not an iterator
= help: the trait `std::iter::Iterator` is not implemented for
= note: required by `std::iter::IntoIterator::into_iter`到目前为止,代码看起来是这样的
use rayon::prelude::*;
use std::fs;
fn main() {
let file_content = match fs::read_to_string("input.txt") {
Ok(s) => s,
Err(e) => {
eprintln!("Error: {}", e);
std::process::exit(1);
}
};
file_content = 5;
for line in file_content.par_lines() {
println!("{}", line);
}
std::process::exit(0);
}我是否遗漏了一个特征定义?我如何修复这个错误?
发布于 2019-08-23 17:22:59
不能将并行迭代器与(非并行) for循环一起使用。
相反,在并行迭代器上使用.for_each(|| …)回调。
或者,先调用.collect::<Vec<_>>(),然后调用非并行for。
对于更高级的情况,您还可以将结果并行发送到channel,然后使用非并行for从通道中读取。
https://stackoverflow.com/questions/57419699
复制相似问题