Skip to content

Commit 3342ae7

Browse files
committed
Implement get_pair for BTreeMap
1 parent 60fcc1a commit 3342ae7

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/liballoc/btree/map.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,35 @@ impl<K: Ord, V> BTreeMap<K, V> {
576576
}
577577
}
578578

579+
/// Returns a references to the key-value pair corresponding to the supplied
580+
/// key.
581+
///
582+
/// The supplied key may be any borrowed form of the map's key type, but the ordering
583+
/// on the borrowed form *must* match the ordering on the key type.
584+
///
585+
/// # Examples
586+
///
587+
/// ```
588+
/// #![feature(map_get_pair)]
589+
/// use std::collections::BTreeMap;
590+
///
591+
/// let mut map = BTreeMap::new();
592+
/// map.insert(1, "a");
593+
/// assert_eq!(map.get_pair(&1), Some((&1, &"a")));
594+
/// assert_eq!(map.get_pair(&2), None);
595+
/// ```
596+
#[unstable(feature = "map_get_pair", issue = "43143")]
597+
#[inline]
598+
pub fn get_pair<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
599+
where K: Borrow<Q>,
600+
Q: Ord
601+
{
602+
match search::search_tree(self.root.as_ref(), key) {
603+
Found(handle) => Some(handle.into_kv()),
604+
GoDown(_) => None,
605+
}
606+
}
607+
579608
/// Returns `true` if the map contains a value for the specified key.
580609
///
581610
/// The key may be any borrowed form of the map's key type, but the ordering

src/libstd/collections/hash/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,15 +1134,15 @@ impl<K, V, S> HashMap<K, V, S>
11341134
/// # Examples
11351135
///
11361136
/// ```
1137-
/// #![feature(hashmap_get_pair)]
1137+
/// #![feature(map_get_pair)]
11381138
/// use std::collections::HashMap;
11391139
///
11401140
/// let mut map = HashMap::new();
11411141
/// map.insert(1, "a");
11421142
/// assert_eq!(map.get_pair(&1), Some((&1, &"a")));
11431143
/// assert_eq!(map.get_pair(&2), None);
11441144
/// ```
1145-
#[unstable(feature = "hashmap_get_pair", issue = "43143")]
1145+
#[unstable(feature = "map_get_pair", issue = "43143")]
11461146
#[inline]
11471147
pub fn get_pair<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
11481148
where K: Borrow<Q>,

0 commit comments

Comments
 (0)