From 00ca3fe73115614ee806113b19b51ffad83d92d6 Mon Sep 17 00:00:00 2001 From: "Chai T. Rex" Date: Sun, 18 Oct 2020 15:45:09 -0400 Subject: [PATCH 1/4] Stabilize or_insert_with_key --- collections/btree/map/entry.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/collections/btree/map/entry.rs b/collections/btree/map/entry.rs index 73a0ca2..3ff648f 100644 --- a/collections/btree/map/entry.rs +++ b/collections/btree/map/entry.rs @@ -116,7 +116,6 @@ impl<'a, K: Ord, V> Entry<'a, K, V> { } } - #[unstable(feature = "or_insert_with_key", issue = "71024")] /// Ensures a value is in the entry by inserting, if empty, the result of the default function, /// which takes the key as its argument, and returns a mutable reference to the value in the /// entry. @@ -124,7 +123,6 @@ impl<'a, K: Ord, V> Entry<'a, K, V> { /// # Examples /// /// ``` - /// #![feature(or_insert_with_key)] /// use std::collections::BTreeMap; /// /// let mut map: BTreeMap<&str, usize> = BTreeMap::new(); @@ -134,6 +132,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> { /// assert_eq!(map["poneyland"], 9); /// ``` #[inline] + #[stable(feature = "or_insert_with_key", since = "1.49.0")] pub fn or_insert_with_key V>(self, default: F) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), From 5c008a15d995d8728a01a43428c21d07eb2a2f3d Mon Sep 17 00:00:00 2001 From: "Chai T. Rex" Date: Tue, 1 Dec 2020 01:06:40 -0500 Subject: [PATCH 2/4] Update rustc version that or_insert_with_key landed --- collections/btree/map/entry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collections/btree/map/entry.rs b/collections/btree/map/entry.rs index 3ff648f..77c285e 100644 --- a/collections/btree/map/entry.rs +++ b/collections/btree/map/entry.rs @@ -132,7 +132,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> { /// assert_eq!(map["poneyland"], 9); /// ``` #[inline] - #[stable(feature = "or_insert_with_key", since = "1.49.0")] + #[stable(feature = "or_insert_with_key", since = "1.50.0")] pub fn or_insert_with_key V>(self, default: F) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), From 25fee2600af6bfc18549e081f4899dadb1807259 Mon Sep 17 00:00:00 2001 From: "Chai T. Rex" Date: Mon, 7 Dec 2020 21:36:01 -0500 Subject: [PATCH 3/4] Improved documentation for HashMap/BTreeMap Entry's .or_insert_with_key method --- collections/btree/map/entry.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/collections/btree/map/entry.rs b/collections/btree/map/entry.rs index 77c285e..9dd68e2 100644 --- a/collections/btree/map/entry.rs +++ b/collections/btree/map/entry.rs @@ -116,9 +116,12 @@ impl<'a, K: Ord, V> Entry<'a, K, V> { } } - /// Ensures a value is in the entry by inserting, if empty, the result of the default function, - /// which takes the key as its argument, and returns a mutable reference to the value in the - /// entry. + /// Ensures a value is in the entry by inserting, if empty, the result of the default function. + /// This method allows for generating key-derived values for insertion by providing the default + /// function a reference to the key that was moved during the `.entry(key)` method call.
+ /// + /// The reference to the moved key is provided so that cloning or copying the key is + /// unnecessary, unlike with `.or_insert_with(|| ... )`. /// /// # Examples /// From ee5b38313adf8421542d9c772f03c293681b77b1 Mon Sep 17 00:00:00 2001 From: "Chai T. Rex" Date: Mon, 7 Dec 2020 21:59:52 -0500 Subject: [PATCH 4/4] Removed spurious linebreak from new documentation --- collections/btree/map/entry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collections/btree/map/entry.rs b/collections/btree/map/entry.rs index 9dd68e2..6626124 100644 --- a/collections/btree/map/entry.rs +++ b/collections/btree/map/entry.rs @@ -118,7 +118,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> { /// Ensures a value is in the entry by inserting, if empty, the result of the default function. /// This method allows for generating key-derived values for insertion by providing the default - /// function a reference to the key that was moved during the `.entry(key)` method call.
+ /// function a reference to the key that was moved during the `.entry(key)` method call. /// /// The reference to the moved key is provided so that cloning or copying the key is /// unnecessary, unlike with `.or_insert_with(|| ... )`.