如何解决如何将 hashbrown 数据类型与 Rayon 并行迭代器一起使用?
我有一个 hashbrown::HashSet
,我正在尝试将 Rayon 的 par_iter
与它一起使用,但我无法找出正确的语法。
Cargo.toml
[package]
name = "basic"
version = "0.1.0"
authors = ["Me <me@example.com>"]
edition = "2018"
# See more keys and their deFinitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hashbrown = "0.9.1"
rayon = "1.5.0"
src/main.rs
use hashbrown::*;
use rayon::prelude::*;
// use hashbrown::hash_set::rayon::*;
fn main() {
println!("Hello,world!");
let hs = HashSet::new();
for n in 1..100 {
hs.insert(n);
}
hs.par_iter().for_each(|n| println!("n={}",n));
}
编译失败。
error[E0599]: no method named `par_iter` found for struct `hashbrown::HashSet<{integer}>` in the current scope
--> src/main.rs:13:8
|
13 | hs.par_iter().for_each(|n| println!("n={}",n));
| ^^^^^^^^ method not found in `hashbrown::HashSet<{integer}>`
|
::: /Users/me/.cargo/registry/src/github.com-1ecc6299db9ec823/hashbrown-0.9.1/src/set.rs:114:1
|
114 | pub struct HashSet<T,S = DefaultHashBuilder> {
| --------------------------------------------- doesn't satisfy `_: rayon::iter::IntoParallelRefIterator`
|
= note: the method `par_iter` exists but the following trait bounds were not satisfied:
`&hashbrown::HashSet<{integer}>: IntoParallelIterator`
which is required by `hashbrown::HashSet<{integer}>: rayon::iter::IntoParallelRefIterator`
warning: unused import: `rayon::prelude`
--> src/main.rs:2:5
|
2 | use rayon::prelude::*;
| ^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
hashset-rayon docs 表明 hashbrown::hash_set::rayon
具有与 Rayon 相关的数据类型,我知道有时我需要使用特征来使功能可用。但是,如果我取消注释导入,它甚至找不到模块:
error[E0432]: unresolved import `hashbrown::hash_set::rayon`
--> src/main.rs:3:26
|
3 | use hashbrown::hash_set::rayon::*;
| ^^^^^ Could not find `rayon` in `hash_set`
我在 Mac 上运行 Rust 1.49.0:
$ rustup show
Default host: x86_64-apple-darwin
rustup home: /Users/tda0106/.rustup
installed toolchains
--------------------
stable-x86_64-apple-darwin (default)
nightly-x86_64-apple-darwin
active toolchain
----------------
stable-x86_64-apple-darwin (default)
rustc 1.49.0 (e1884a8e3 2020-12-29)
有什么问题?
编辑
正如策展人在评论中指出的@E_net4,人造丝支持是一项功能。将依赖项更改为
[dependencies]
hashbrown = { version = "0.9.1",features = ["rayon"] }
rayon = "1.5.0"
无需额外的 use 语句即可完成这项工作。
我不清楚文档在哪里指出这一点。
解决方法
如果您查看 hashbrown
crate 的内部,您会看到 rayon
支持仅在启用 rayon
功能时才存在。您可以在 Cargo.toml
中启用该功能:
[dependencies]
hashbrown = { version = "0.9.1",features = ["rayon"] }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。