Using fxhasher crate

#1

When I try to create a new FxHashMap, I get:

no function or associated item named new found for type std::collections::HashMap<_, _, std::hash::BuildHasherDefault<fxhash::FxHasher>> in the current scope
–> src/main.rs:2:5
|
2 | fxhash::FxHashMap::new();
| ^^^^^^^^^^^^^^^^^^^^^^ function or associated item not found in std::collections::HashMap<_, _, std::hash::BuildHasherDefault<fxhash::FxHasher>>

Neither the error, nor the crate’s docs say anything about any extra traits or other methods needed to use it.

Did it work? Is the crate broken? Has stdlib changed?

0 Likes

#2

HashMap::new is only implemented for RandomState, so you have to use FxHashMap::default() instead to create an empty one.

0 Likes

#3

FxHashMap<K, V> is simply an alias for HashMap<K, V, FxBuildHasher>. The new function is only implemented for HashMap's with the default std::collections::hash_map::RandomState hasher builder. Therefore it can’t be used with FxHashMap which uses FxBuildHasher (source).

A HashMap with an arbitrary hasher can be created using the default method instead.

Edit: Oops didn’t see vitalyd`s answer

0 Likes

#4

Thanks.

That’s a shame about the API. It even uses Default::default() internally. I guess it’d break too much to change it to work with any Default.

0 Likes