Troy Daniels
Troy Daniels

Reputation: 3598

How do I use hashbrown data types with Rayon parallel iterators?

I have a hashbrown::HashSet, and I am trying to use Rayon's par_iter with it, but I cannot figure out the right syntax.

Cargo.toml

[package]
name = "basic"
version = "0.1.0"
authors = ["Me <[email protected]>"]
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));
}

This fails to compile.

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

The hashset-rayon docs suggest that hashbrown::hash_set::rayon has a Rayon-related data type, and I know that sometimes I need to use traits to make functionality available. However, if I uncomment the import, it cannot even find the module:

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`

I am running Rust 1.49.0 on a Mac:

$ 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)

What is the problem?

Edit

As @E_net4 the curator pointed out in the comments, the rayon support is in a feature. Changing the dependencies to

[dependencies]
hashbrown = { version = "0.9.1", features = ["rayon"] }
rayon = "1.5.0"

makes this work, without needing the extra use statement.

It's not clear to me where the documentation indicates this.

Upvotes: 2

Views: 758

Answers (1)

Ibraheem Ahmed
Ibraheem Ahmed

Reputation: 13518

If you look inside of the hashbrown crate, you will see that rayon support is only present if the rayon feature is enabled. You can enable the feature in your Cargo.toml:

[dependencies]
hashbrown = { version = "0.9.1", features = ["rayon"] }

Upvotes: 3

Related Questions