2

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 <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));
}

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.

Troy Daniels
  • 3,270
  • 2
  • 25
  • 57
  • 5
    `rayon` support is behind the `rayon` Cargo feature, which you have to enable explicitly in Cargo.toml. Similar question: https://stackoverflow.com/q/55783064 – E_net4 Feb 03 '21 at 17:32
  • That fixed the problem. Is there someplace at https://docs.rs/hashbrown/0.9.1/hashbrown/index.html that tells me I need to do this? – Troy Daniels Feb 03 '21 at 19:20
  • The project is not yet 1.0, and its docs look incomplete (as is typical for pre-1.0 projects), you might add an issue via github where the source lives: https://github.com/rust-lang/hashbrown/issues – Ultrasaurus Feb 05 '21 at 23:09

1 Answers1

2

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"] }
Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54