Skip to content

Commit

Permalink
Rollup merge of rust-lang#132758 - nnethercote:improve-get_key_value-…
Browse files Browse the repository at this point in the history
…docs, r=cuviper

Improve `{BTreeMap,HashMap}::get_key_value` docs.

They are unusual methods. The docs don't really describe the cases when they might be useful (as opposed to just `get`), and the examples don't demonstrate the interesting cases at all.

This commit improves the docs and the examples.
  • Loading branch information
fmease authored Nov 19, 2024
2 parents b71fb5e + 2765432 commit 2226541
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
46 changes: 42 additions & 4 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,20 +677,58 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
}
}

/// Returns the key-value pair corresponding to the supplied key.
/// Returns the key-value pair corresponding to the supplied key. This is
/// potentially useful:
/// - for key types where non-identical keys can be considered equal;
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
/// - for getting a reference to a key with the same lifetime as the collection.
///
/// 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
///
/// ```
/// use std::cmp::Ordering;
/// use std::collections::BTreeMap;
///
/// #[derive(Clone, Copy, Debug)]
/// struct S {
/// id: u32,
/// # #[allow(unused)] // prevents a "field `name` is never read" error
/// name: &'static str, // ignored by equality and ordering operations
/// }
///
/// impl PartialEq for S {
/// fn eq(&self, other: &S) -> bool {
/// self.id == other.id
/// }
/// }
///
/// impl Eq for S {}
///
/// impl PartialOrd for S {
/// fn partial_cmp(&self, other: &S) -> Option<Ordering> {
/// self.id.partial_cmp(&other.id)
/// }
/// }
///
/// impl Ord for S {
/// fn cmp(&self, other: &S) -> Ordering {
/// self.id.cmp(&other.id)
/// }
/// }
///
/// let j_a = S { id: 1, name: "Jessica" };
/// let j_b = S { id: 1, name: "Jess" };
/// let p = S { id: 2, name: "Paul" };
/// assert_eq!(j_a, j_b);
///
/// 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);
/// map.insert(j_a, "Paris");
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
/// assert_eq!(map.get_key_value(&p), None);
/// ```
#[stable(feature = "map_get_key_value", since = "1.40.0")]
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
Expand Down
40 changes: 36 additions & 4 deletions library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,11 @@ where
self.base.get(k)
}

/// Returns the key-value pair corresponding to the supplied key.
/// Returns the key-value pair corresponding to the supplied key. This is
/// potentially useful:
/// - for key types where non-identical keys can be considered equal;
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
/// - for getting a reference to a key with the same lifetime as the collection.
///
/// 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
Expand All @@ -890,11 +894,39 @@ where
///
/// ```
/// use std::collections::HashMap;
/// use std::hash::{Hash, Hasher};
///
/// #[derive(Clone, Copy, Debug)]
/// struct S {
/// id: u32,
/// # #[allow(unused)] // prevents a "field `name` is never read" error
/// name: &'static str, // ignored by equality and hashing operations
/// }
///
/// impl PartialEq for S {
/// fn eq(&self, other: &S) -> bool {
/// self.id == other.id
/// }
/// }
///
/// impl Eq for S {}
///
/// impl Hash for S {
/// fn hash<H: Hasher>(&self, state: &mut H) {
/// self.id.hash(state);
/// }
/// }
///
/// let j_a = S { id: 1, name: "Jessica" };
/// let j_b = S { id: 1, name: "Jess" };
/// let p = S { id: 2, name: "Paul" };
/// assert_eq!(j_a, j_b);
///
/// 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);
/// map.insert(j_a, "Paris");
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
/// assert_eq!(map.get_key_value(&p), None);
/// ```
#[inline]
#[stable(feature = "map_get_key_value", since = "1.40.0")]
Expand Down

0 comments on commit 2226541

Please sign in to comment.