Lazy_static hashmap and fasthash

I am trying to use the fasthash crate within a lazy_static context like as below
If I replace the

let mut map1=HashMap::with_capacity(50000);

things work. I am not able to understand where should the hashmap type be changed ?

lazy_static! {
    
    static ref HASHMAP: HashMap<String,String> = {
        let xxhash = RandomState::<xx::Hash64>::new();
        let mut map1 = HashMap::with_capacity_and_hasher(50000, xxhash);
        let filename = "xxx.txt";
        let file = File::open(&filename).unwrap();
        let mut reader = BufReader::new(file);
        let mut line = String::new();
        loop {
            // populate the hashmap
        }
        map1
    };
}

I get the following error

error[E0308]: mismatched types

|
| static ref HASHMAP: HashMap<String,String> = {
| ---------------------- expected HashMap<std::string::String, std::string::String> because of return type
...
| map1
| ^^^^ expected struct std::collections::hash_map::RandomState, found struct fasthash::RandomState
|
= note: expected struct HashMap<_, _, std::collections::hash_map::RandomState>
found struct HashMap<_, _, fasthash::RandomState<fasthash::xx::Hash64>>

error: aborting due to previous error

For more information about this error, try rustc --explain E0308.

The HashMap type has three generic parameters:

  • The key
  • The value
  • The hasher

It's just that the third parameter has a default value, and if you don't specify it, it picks the default value — it does not try to infer it from context. To fix this, specify the hasher.

-   static ref HASHMAP: HashMap<String, String> = {
+   static ref HASHMAP: HashMap<String, String, fasthash::RandomState<xx::Hash64>> = {

Thanks that fixed it
I was trying fasthash::xx::Hash64 :slight_smile: