Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Hashbrown Hashmap vs Std HashMap (trait Equivalent is not implemented for ...)
Consider the following code:
use hashbrown::HashMap as HBHMap;
use std::collections::HashMap as StdHMap;
#[derive(Hash, Eq, PartialEq)]
struct Foo {
x: i32,
}
fn main() {
// std HashMap
let x = Foo { x: 0 };
let mut hm1 = StdHMap::new();
hm1.insert(x, 100);
let y = &Foo { x: 0 };
hm1.get(y);
hm1.get(&y); // <-- works fine
// hashbrown HashMap
let x = Foo { x: 0 };
let mut hm2 = HBHMap::new();
hm2.insert(x, 100);
let y = &Foo { x: 0 };
hm2.get(y);
hm2.get(&y); // <-- error here
}
I know the example is contrived, but I am having a hard time understanding why the std hasmap example works whereas th hashbrown's hashmap example doesnt?
I checked the trait bound requirements:
std hashmap:
pub fn get<Q>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
hashbrown hashmap:
pub fn get<Q>(&self, k: &Q) -> Option<&V>
where
Q: Hash + Equivalent<K> + ?Sized,
impl<Q, K> Equivalent<K> for Q
where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
What kind of cooercion / auto-deref / auto-borrow is in play here such that the std Hashmap example works but the hashbrown hashmap one gives the following error:
❯ cargo b
Compiling hashbrown-vs-hashmap v0.1.0 (/home/shank/code/tmp/hashbrown-vs-hashmap)
error[E0277]: the trait bound `&Foo: Equivalent<Foo>` is not satisfied
--> src/main.rs:24:13
|
24 | hm2.get(&y);
| --- ^^ the trait `Borrow<&Foo>` is not implemented for `Foo`
| |
| required by a bound introduced by this call
|
= note: required for `&Foo` to implement `Equivalent<Foo>`
note: required by a bound in `hashbrown::HashMap::<K, V, S, A>::get`
--> /home/shank/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hashbrown-0.15.4/src/map.rs:1306:19
|
1304 | pub fn get<Q>(&self, k: &Q) -> Option<&V>
| --- required by a bound in this associated function
1305 | where
1306 | Q: Hash + Equivalent<K> + ?Sized,
| ^^^^^^^^^^^^^ required by this bound in `HashMap::<K, V, S, A>::get`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `hashbrown-vs-hashmap` (bin "hashbrown-vs-hashmap") due to 1 previous error
1 post - 1 participant
🏷️ rust_feed