@@ -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 ) >
0 commit comments