首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在人造丝并行迭代器中使用哈希布朗数据类型?

如何在人造丝并行迭代器中使用哈希布朗数据类型?
EN

Stack Overflow用户
提问于 2021-02-03 17:25:23
回答 1查看 338关注 0票数 2

我有一个hashbrown::HashSet,我试着用它来使用人造丝的par_iter,但是我找不出正确的语法。

Cargo.toml

代码语言:javascript
复制
[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

代码语言:javascript
复制
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));
}

这无法编译。

代码语言:javascript
复制
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

哈希集-人造丝文档建议hashbrown::hash_set::rayon有一个与人造丝相关的数据类型,而且我知道有时我需要使用特性来使功能可用。但是,如果取消对导入的注释,它甚至找不到模块:

代码语言:javascript
复制
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:

代码语言:javascript
复制
$ 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在评论中指出的那样,人造丝支持是在一个特性中。将依赖项更改为

代码语言:javascript
复制
[dependencies]
hashbrown = { version = "0.9.1", features = ["rayon"] }
rayon = "1.5.0"

使此工作,而不需要额外的使用语句。

我不清楚文档在哪里表明了这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-05 20:15:52

如果您查看板条箱内部,您将看到只有当启用了rayon特性时,才会出现rayon支持。您可以在Cargo.toml中启用该功能。

代码语言:javascript
复制
[dependencies]
hashbrown = { version = "0.9.1", features = ["rayon"] }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66032586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档