I'm writing a hashmap for fun and ran into an interesting problem
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
let bucket_idx = self.bucket(&key);
if let Some(entry) = self.buckets[bucket_idx]
.iter_mut()
.find(|(k, _)| k == &key)
{
// OccEntry takes a &'a mut (K, V)
return Entry::Occupied( OccEntry { entry } );
};
// takes the key: K and bucket: &'a mut Vec<(K, V)>
Entry::Vacant( VacEntry { key, bucket: &mut self.buckets[bucket_idx], } )
}
I then spent an hour trying to recreate it in the playground... I can't I'm sure it's me but I do not understand why this is ok but not hash-map's entry fn
.
fn test(&mut self, key: char) -> Either<'_> {
self.take_ref();
if let Some(s) = self.little.iter_mut().find(|c| *c == &key) {
return Either::A( Other((s, 0)) );
};
Either::B( Example( &mut self.all ))
}
Thanks all!!