@@ -312,8 +312,12 @@ impl<K: Ord, V> BTreeMap<K, V> {
312312 // 2) While ODS may potentially return the pair we *just* inserted after
313313 // the split, we will never do this. Again, this shouldn't effect the analysis.
314314
315- /// Inserts a key-value pair into the map. If the key already had a value
316- /// present in the map, that value is returned. Otherwise, `None` is returned.
315+ /// Inserts a key-value pair into the map.
316+ ///
317+ /// If the map did not have this key present, `None` is returned.
318+ ///
319+ /// If the map did have this key present, that value is returned, and the
320+ /// entry is not updated. See the examples below for more.
317321 ///
318322 /// # Examples
319323 ///
@@ -328,6 +332,50 @@ impl<K: Ord, V> BTreeMap<K, V> {
328332 /// assert_eq!(map.insert(37, "c"), Some("b"));
329333 /// assert_eq!(map[&37], "c");
330334 /// ```
335+ ///
336+ /// If we have a more complex key, calls to `insert()` will
337+ /// not update the value of the key. For example:
338+ ///
339+ /// ```
340+ /// use std::cmp::Ordering;
341+ /// use std::collections::BTreeMap;
342+ /// use std::hash::{Hash, Hasher};
343+ ///
344+ /// #[derive(Debug)]
345+ /// struct Foo {
346+ /// a: u32,
347+ /// b: &'static str,
348+ /// }
349+ ///
350+ /// // we will compare `Foo`s by their `a` value only.
351+ /// impl PartialEq for Foo {
352+ /// fn eq(&self, other: &Self) -> bool { self.a == other.a }
353+ /// }
354+ ///
355+ /// impl Eq for Foo {}
356+ ///
357+ /// // we will hash `Foo`s by their `a` value only.
358+ /// impl Hash for Foo {
359+ /// fn hash<H: Hasher>(&self, h: &mut H) { self.a.hash(h); }
360+ /// }
361+ ///
362+ /// impl PartialOrd for Foo {
363+ /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.a.partial_cmp(&other.a) }
364+ /// }
365+ ///
366+ /// impl Ord for Foo {
367+ /// fn cmp(&self, other: &Self) -> Ordering { self.a.cmp(&other.a) }
368+ /// }
369+ ///
370+ /// let mut map = BTreeMap::new();
371+ /// map.insert(Foo { a: 1, b: "baz" }, ());
372+ ///
373+ /// // We already have a Foo with an a of 1, so this will be updating the value.
374+ /// map.insert(Foo { a: 1, b: "xyz" }, ());
375+ ///
376+ /// // ... but the key hasn't changed. b is still "baz", not "xyz"
377+ /// assert_eq!(map.keys().next().unwrap().b, "baz");
378+ /// ```
331379 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
332380 pub fn insert ( & mut self , mut key : K , mut value : V ) -> Option < V > {
333381 // This is a stack of rawptrs to nodes paired with indices, respectively
0 commit comments