Created
December 30, 2019 18:46
-
-
Save Youka/759662b1cb6b5d78353a8f5ff4cfa3ba to your computer and use it in GitHub Desktop.
Map `get_key_value` implementation before Rust v1.40.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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