about summary refs log tree commit diff
path: root/src/libstd
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/libstd
parentf5631d9ac7745dd6eaea2bc6c236d5f8e54e9a18 (diff)
downloadrust-fbec3ec5a78747ee458518e4be7cfe1b5eac9e3b.tar.gz
rust-fbec3ec5a78747ee458518e4be7cfe1b5eac9e3b.zip
Implement get_key_value for HashMap, BTreeMap
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/collections/hash/map.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs
index b18b38ec302..f0bb781411f 100644
--- a/src/libstd/collections/hash/map.rs
+++ b/src/libstd/collections/hash/map.rs
@@ -1184,6 +1184,34 @@ impl<K, V, S> HashMap<K, V, S>
         self.search(k).map(|bucket| bucket.into_refs().1)
     }
 
+    /// 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
+    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
+    /// the key type.
+    ///
+    /// [`Eq`]: ../../std/cmp/trait.Eq.html
+    /// [`Hash`]: ../../std/hash/trait.Hash.html
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(map_get_key_value)]
+    /// use std::collections::HashMap;
+    ///
+    /// let mut map = HashMap::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: Hash + Eq
+    {
+        self.search(k).map(|bucket| bucket.into_refs())
+    }
+
     /// 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