Using fxhasher crate

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/
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?

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

1 Like

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

1 Like


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.