Skip to content

Instantly share code, notes, and snippets.

@Youka
Created December 30, 2019 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Youka/759662b1cb6b5d78353a8f5ff4cfa3ba to your computer and use it in GitHub Desktop.
Save Youka/759662b1cb6b5d78353a8f5ff4cfa3ba to your computer and use it in GitHub Desktop.
Map `get_key_value` implementation before Rust v1.40.0
// On stable: <https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.get_key_value>
pub fn get_key_value<'a,K,V,Q: ?Sized>(map: &'a HashMap<K,V>, k: &Q) -> Option<(&'a K, &'a V)>
where K: std::borrow::Borrow<Q> + std::hash::Hash + std::cmp::Eq,
Q: std::hash::Hash + Eq {
let key = map.keys().find(|key| key.borrow() == k)?;
Some((key, map.get(key.borrow())?))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment