有没有办法检测人造丝中的溢出并迫使它恐慌而不是无限循环?
extern crate rayon;
use rayon::prelude::*;
fn main() {
let sample: Vec<u32> = (0..50000000).collect();
let sum: u32 = sample.par_iter().sum();
println!("{}",sum );
}
有没有办法检测人造丝中的溢出并迫使它恐慌而不是无限循环?
extern crate rayon;
use rayon::prelude::*;
fn main() {
let sample: Vec<u32> = (0..50000000).collect();
let sum: u32 = sample.par_iter().sum();
println!("{}",sum );
}
您正在寻找ParallelIterator::try_reduce
. 文档示例可以满足您的需求(以及更多):
use rayon::prelude::*; // Compute the sum of squares, being careful about overflow. fn sum_squares<I: IntoParallelIterator<Item = i32>>(iter: I) -> Option<i32> { iter.into_par_iter() .map(|i| i.checked_mul(i)) // square each item, .try_reduce(|| 0, i32::checked_add) // and add them up! } assert_eq!(sum_squares(0..5), Some(0 + 1 + 4 + 9 + 16)); // The sum might overflow assert_eq!(sum_squares(0..10_000), None); // Or the squares might overflow before it even reaches `try_reduce` assert_eq!(sum_squares(1_000_000..1_000_001), None);
专门针对您的示例:
extern crate rayon;
use rayon::prelude::*;
fn main() {
let sample: Vec<u32> = (0..50000000).collect();
let sum = sample
.into_par_iter()
.map(Some)
.try_reduce(
|| 0,
|a, b| a.checked_add(b)
);
println!("{:?}", sum);
}
这collect
是不必要的低效率,但我现在已经离开了。