How to have a string hasher with preinitialized state just from std:: in Rust?

Viewed 47

Is it possible to have a hasher which does not use random initial state without having to import any crates? I'm browsing the docs but I'm not finding a solution. It seems like the only option involves RandomState

This does not have to be any fancy hasher. I'll even to uniqeness check afterwards to make sure I don't have collisions.

1 Answers

Use BuildHasherDefault. It constructs a Hasher using its Default implementation.

For example,

use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;

type DefaultedHashMap<K, V> = HashMap<K, V, BuildHasherDefault<DefaultHasher>>;

let map = DefaultedHashMap::default();

Or:

use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;

let map = HashMap::with_hasher(BuildHasherDefault::<DefaultHasher>::default()); 
Related