January 2019
Intermediate to advanced
316 pages
8h 8m
English
Just like inserting, the retrieval process has the same steps. Whether the get() function to return a value or the remove() function, both go through the same steps: hash, match a bucket, do a linear search, and lastly, match with the expected return type. The get() function can utilize Rust's powerful iterators by using find to match the predicate within a bucket's vector and, since an Option<Item> is returned, its map function to extract the value instead of returning the entire pair:
pub fn get(&self, key: &K) -> Option<V> { let h = (self.hash_fn)(key); let idx = h & (self.store.len() - 1); self.store[idx] .iter() .find(|e| e.0 == *key) .map(|e| e.1.clone())}pub fn remove(&mut self, key: K) -> Option<V> { let h = (self.hash_fn)(&key); ...Read now
Unlock full access