以下代码可以通过编译:
use rayon::iter::IntoParallelRefIterator;
fn main() {
(&vec![1,2,3]).par_iter();
}
我不理解为何下述代码无法通过编译:
use rayon::prelude::*;
trait VecExt<T, R> {
fn map(&self, f: impl Fn(&T) -> R) -> Vec<R>;
}
impl<T, R> VecExt<T, R> for Vec<T> {
fn map(&self, f: impl Fn(&T) -> R) -> Vec<R> {
self.par_iter().map(f).collect()
}
}
获得错误信息:
no method named `par_iter` found for reference `&Vec<T>` in the current scope
the method `par_iter` exists but the following trait bounds were not satisfied:
`&Vec<T>: IntoParallelIterator`
which is required by `Vec<T>: rayon::iter::IntoParallelRefIterator`
`&&Vec<T>: IntoParallelIterator`
which is required by `&Vec<T>: rayon::iter::IntoParallelRefIterator`
`&[T]: IntoParallelIterator`
which is required by `[T]: rayon::iter::IntoParallelRefIterator`
1
共 1 条评论, 1 页
评论区
写评论少了Sync和Send的约束