Skip to content

Commit ea41137

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 fbab782 commit ea41137

File tree

2 files changed

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

2 files changed

+72
-8
lines changed

library/alloc/src/collections/btree/map.rs

+39-4
Original file line numberDiff line numberDiff line change
@@ -705,20 +705,55 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
705705
}
706706
}
707707

708-
/// Returns the key-value pair corresponding to the supplied key.
708+
/// Returns the key-value pair corresponding to the supplied key. This is
709+
/// only useful for key types where non-identical keys can be considered equal.
709710
///
710711
/// The supplied key may be any borrowed form of the map's key type, but the ordering
711712
/// on the borrowed form *must* match the ordering on the key type.
712713
///
713714
/// # Examples
714715
///
715716
/// ```
717+
/// use std::cmp::Ordering;
716718
/// use std::collections::BTreeMap;
717719
///
720+
/// #[derive(Clone, Copy, Debug)]
721+
/// struct S {
722+
/// id: u32,
723+
/// # #[allow(unused)] // prevents a "field `name` is never read" error
724+
/// name: &'static str, // ignored by equality and ordering operations
725+
/// }
726+
///
727+
/// impl PartialEq for S {
728+
/// fn eq(&self, other: &S) -> bool {
729+
/// self.id == other.id
730+
/// }
731+
/// }
732+
///
733+
/// impl Eq for S {}
734+
///
735+
/// impl PartialOrd for S {
736+
/// fn partial_cmp(&self, other: &S) -> Option<Ordering> {
737+
/// self.id.partial_cmp(&other.id)
738+
/// }
739+
/// }
740+
///
741+
/// impl Ord for S {
742+
/// fn cmp(&self, other: &S) -> Ordering {
743+
/// self.id.cmp(&other.id)
744+
/// }
745+
/// }
746+
///
747+
/// let j_a = S { id: 1, name: "Jessica" };
748+
/// let j_b = S { id: 1, name: "Jess" };
749+
/// let p = S { id: 2, name: "Paul" };
750+
/// assert_eq!(j_a, j_b);
751+
///
718752
/// let mut map = BTreeMap::new();
719-
/// map.insert(1, "a");
720-
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
721-
/// assert_eq!(map.get_key_value(&2), None);
753+
/// map.insert(j_a, "Paris");
754+
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
755+
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
756+
/// assert_eq!(map.get_key_value(&p), None);
722757
/// ```
723758
#[stable(feature = "map_get_key_value", since = "1.40.0")]
724759
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>

library/std/src/collections/hash/map.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,8 @@ 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+
/// only useful for key types where non-identical keys can be considered equal.
884885
///
885886
/// The supplied key may be any borrowed form of the map's key type, but
886887
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
@@ -890,11 +891,39 @@ where
890891
///
891892
/// ```
892893
/// use std::collections::HashMap;
894+
/// use std::hash::{Hash, Hasher};
895+
///
896+
/// #[derive(Clone, Copy, Debug)]
897+
/// struct S {
898+
/// id: u32,
899+
/// # #[allow(unused)] // prevents a "field `name` is never read" error
900+
/// name: &'static str, // ignored by equality and hashing operations
901+
/// }
902+
///
903+
/// impl PartialEq for S {
904+
/// fn eq(&self, other: &S) -> bool {
905+
/// self.id == other.id
906+
/// }
907+
/// }
908+
///
909+
/// impl Eq for S {}
910+
///
911+
/// impl Hash for S {
912+
/// fn hash<H: Hasher>(&self, state: &mut H) {
913+
/// self.id.hash(state);
914+
/// }
915+
/// }
916+
///
917+
/// let j_a = S { id: 1, name: "Jessica" };
918+
/// let j_b = S { id: 1, name: "Jess" };
919+
/// let p = S { id: 2, name: "Paul" };
920+
/// assert_eq!(j_a, j_b);
893921
///
894922
/// 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);
923+
/// map.insert(j_a, "Paris");
924+
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
925+
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
926+
/// assert_eq!(map.get_key_value(&p), None);
898927
/// ```
899928
#[inline]
900929
#[stable(feature = "map_get_key_value", since = "1.40.0")]

0 commit comments

Comments
 (0)