njhkugk6i76g6gi6gi7g6
njhkugk6i76g6gi6gi7g6

Reputation: 61

Can't use the insert() method

I have a very strange problem: When i create a HashMap with HashMap::new(), everything works right, but when i create a hashmap with HashMap::capacity_and_hasher, i can't use insert method

the error:

    |
 93 | map.insert(1,2);
    |     ^^^^^^ method cannot be called on `HashMap<_, _, &RandomState>` due to unsatisfied trait bounds
    |
   = note: the following trait bounds were not satisfied:
           `&RandomState: BuildHasher`

The code:

let s = RandomState::new();
let mut map = HashMap::with_hasher(&s);
map.insert(1,2);

Upvotes: 2

Views: 383

Answers (1)

Alexey S. Larionov
Alexey S. Larionov

Reputation: 7927

Pass your object s of type RandomState by value. Playground

Notice HashMap::with_hasher(s);

use std::collections::HashMap;
use std::collections::hash_map::RandomState;

fn main() {
    let s = RandomState::new();
    let mut map = HashMap::with_hasher(s);
    map.insert(1,2);
}

The very same is suggested by documentation of with_hasher

Upvotes: 3

Related Questions