Skip to content

Commit 60fcc1a

Browse files
committed
Implement get_pair for HashMap
1 parent def3269 commit 60fcc1a

File tree

1 file changed

+30
-0
lines changed
  • src/libstd/collections/hash

1 file changed

+30
-0
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,36 @@ impl<K, V, S> HashMap<K, V, S>
11211121
self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs().1)
11221122
}
11231123

1124+
/// Returns a references to the key-value pair corresponding to the supplied
1125+
/// key.
1126+
///
1127+
/// The supplied key may be any borrowed form of the map's key type, but
1128+
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
1129+
/// the key type.
1130+
///
1131+
/// [`Eq`]: ../../std/cmp/trait.Eq.html
1132+
/// [`Hash`]: ../../std/hash/trait.Hash.html
1133+
///
1134+
/// # Examples
1135+
///
1136+
/// ```
1137+
/// #![feature(hashmap_get_pair)]
1138+
/// use std::collections::HashMap;
1139+
///
1140+
/// let mut map = HashMap::new();
1141+
/// map.insert(1, "a");
1142+
/// assert_eq!(map.get_pair(&1), Some((&1, &"a")));
1143+
/// assert_eq!(map.get_pair(&2), None);
1144+
/// ```
1145+
#[unstable(feature = "hashmap_get_pair", issue = "43143")]
1146+
#[inline]
1147+
pub fn get_pair<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
1148+
where K: Borrow<Q>,
1149+
Q: Hash + Eq
1150+
{
1151+
self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs())
1152+
}
1153+
11241154
/// Returns true if the map contains a value for the specified key.
11251155
///
11261156
/// The key may be any borrowed form of the map's key type, but

0 commit comments

Comments
 (0)