Looking up entries is part of the insert process of HashMap and it relies on the same functions to provide a suitable entry instance to add data. Just like the insertion process, the lookup process does almost the same, save some steps in the end, listed as follows:
- Create a hash of the key
- Find the hash's bucket in the table
- Move away from the bucket comparing keys (linear search) until found
Since all of this has already been implemented for use in other functions, get() is pretty short, shown in the following code:
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
where K: Borrow<Q>,
Q: Hash + Eq
{
self.search(k).map(|bucket| bucket.into_refs().1)
}
Similarly, the remove function requires search, and removal is implemented on the entry type.