about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorDiggory Blake <diggsey@googlemail.com>2017-12-25 00:00:04 +0000
committerDiggory Blake <diggsey@googlemail.com>2018-03-25 23:50:47 +0100
commitfbec3ec5a78747ee458518e4be7cfe1b5eac9e3b (patch)
tree9f9486bf7a74f2172b96bcc6fb7ab58e1610d6dc /src/liballoc
parentf5631d9ac7745dd6eaea2bc6c236d5f8e54e9a18 (diff)
downloadrust-fbec3ec5a78747ee458518e4be7cfe1b5eac9e3b.tar.gz
rust-fbec3ec5a78747ee458518e4be7cfe1b5eac9e3b.zip
Implement get_key_value for HashMap, BTreeMap
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/btree/map.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs
index ed9c8c18f0d..cada190032a 100644
--- a/src/liballoc/btree/map.rs
+++ b/src/liballoc/btree/map.rs
@@ -576,6 +576,33 @@ impl<K: Ord, V> BTreeMap<K, V> {
         }
     }
 
+    /// Returns the key-value pair corresponding to the supplied key.
+    ///
+    /// The supplied key may be any borrowed form of the map's key type, but the ordering
+    /// on the borrowed form *must* match the ordering on the key type.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(map_get_key_value)]
+    /// use std::collections::BTreeMap;
+    ///
+    /// let mut map = BTreeMap::new();
+    /// map.insert(1, "a");
+    /// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
+    /// assert_eq!(map.get_key_value(&2), None);
+    /// ```
+    #[unstable(feature = "map_get_key_value", issue = "49347")]
+    pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
+        where K: Borrow<Q>,
+              Q: Ord
+    {
+        match search::search_tree(self.root.as_ref(), k) {
+            Found(handle) => Some(handle.into_kv()),
+            GoDown(_) => None,
+        }
+    }
+
     /// Returns `true` if the map contains a value for the specified key.
     ///
     /// The key may be any borrowed form of the map's key type, but the ordering