Skip to content

Commit a33f889

Browse files
committed
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.
1 parent ddcabfe commit a33f889

File tree

2 files changed

+78
-8
lines changed
  • alloc/src/collections/btree
  • std/src/collections/hash

2 files changed

+78
-8
lines changed

alloc/src/collections/btree/map.rs

+42-4
Original file line numberDiff line numberDiff line change
@@ -677,20 +677,58 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
677677
}
678678
}
679679

680-
/// Returns the key-value pair corresponding to the supplied key.
680+
/// Returns the key-value pair corresponding to the supplied key. This is
681+
/// potentially useful:
682+
/// - for key types where non-identical keys can be considered equal;
683+
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
684+
/// - for getting a reference to a key with the same lifetime as the collection.
681685
///
682686
/// The supplied key may be any borrowed form of the map's key type, but the ordering
683687
/// on the borrowed form *must* match the ordering on the key type.
684688
///
685689
/// # Examples
686690
///
687691
/// ```
692+
/// use std::cmp::Ordering;
688693
/// use std::collections::BTreeMap;
689694
///
695+
/// #[derive(Clone, Copy, Debug)]
696+
/// struct S {
697+
/// id: u32,
698+
/// # #[allow(unused)] // prevents a "field `name` is never read" error
699+
/// name: &'static str, // ignored by equality and ordering operations
700+
/// }
701+
///
702+
/// impl PartialEq for S {
703+
/// fn eq(&self, other: &S) -> bool {
704+
/// self.id == other.id
705+
/// }
706+
/// }
707+
///
708+
/// impl Eq for S {}
709+
///
710+
/// impl PartialOrd for S {
711+
/// fn partial_cmp(&self, other: &S) -> Option<Ordering> {
712+
/// self.id.partial_cmp(&other.id)
713+
/// }
714+
/// }
715+
///
716+
/// impl Ord for S {
717+
/// fn cmp(&self, other: &S) -> Ordering {
718+
/// self.id.cmp(&other.id)
719+
/// }
720+
/// }
721+
///
722+
/// let j_a = S { id: 1, name: "Jessica" };
723+
/// let j_b = S { id: 1, name: "Jess" };
724+
/// let p = S { id: 2, name: "Paul" };
725+
/// assert_eq!(j_a, j_b);
726+
///
690727
/// let mut map = BTreeMap::new();
691-
/// map.insert(1, "a");
692-
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
693-
/// assert_eq!(map.get_key_value(&2), None);
728+
/// map.insert(j_a, "Paris");
729+
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
730+
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
731+
/// assert_eq!(map.get_key_value(&p), None);
694732
/// ```
695733
#[stable(feature = "map_get_key_value", since = "1.40.0")]
696734
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>

std/src/collections/hash/map.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,11 @@ where
880880
self.base.get(k)
881881
}
882882

883-
/// Returns the key-value pair corresponding to the supplied key.
883+
/// Returns the key-value pair corresponding to the supplied key. This is
884+
/// potentially useful:
885+
/// - for key types where non-identical keys can be considered equal;
886+
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
887+
/// - for getting a reference to a key with the same lifetime as the collection.
884888
///
885889
/// The supplied key may be any borrowed form of the map's key type, but
886890
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
@@ -890,11 +894,39 @@ where
890894
///
891895
/// ```
892896
/// use std::collections::HashMap;
897+
/// use std::hash::{Hash, Hasher};
898+
///
899+
/// #[derive(Clone, Copy, Debug)]
900+
/// struct S {
901+
/// id: u32,
902+
/// # #[allow(unused)] // prevents a "field `name` is never read" error
903+
/// name: &'static str, // ignored by equality and hashing operations
904+
/// }
905+
///
906+
/// impl PartialEq for S {
907+
/// fn eq(&self, other: &S) -> bool {
908+
/// self.id == other.id
909+
/// }
910+
/// }
911+
///
912+
/// impl Eq for S {}
913+
///
914+
/// impl Hash for S {
915+
/// fn hash<H: Hasher>(&self, state: &mut H) {
916+
/// self.id.hash(state);
917+
/// }
918+
/// }
919+
///
920+
/// let j_a = S { id: 1, name: "Jessica" };
921+
/// let j_b = S { id: 1, name: "Jess" };
922+
/// let p = S { id: 2, name: "Paul" };
923+
/// assert_eq!(j_a, j_b);
893924
///
894925
/// let mut map = HashMap::new();
895-
/// map.insert(1, "a");
896-
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
897-
/// assert_eq!(map.get_key_value(&2), None);
926+
/// map.insert(j_a, "Paris");
927+
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
928+
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
929+
/// assert_eq!(map.get_key_value(&p), None);
898930
/// ```
899931
#[inline]
900932
#[stable(feature = "map_get_key_value", since = "1.40.0")]

0 commit comments

Comments
 (0)