From 1b98f6da7af8cea31066588776b7190c511455b1 Mon Sep 17 00:00:00 2001 From: Alexis Beingessner Date: Fri, 20 Mar 2015 13:43:01 -0400 Subject: default => or_insert per RFC --- src/libstd/collections/hash/map.rs | 7 ++++--- src/libstd/collections/mod.rs | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index b3557529a66..91225891338 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1489,7 +1489,8 @@ impl<'a, K, V> Entry<'a, K, V> { #[unstable(feature = "std_misc", reason = "will soon be replaced by or_insert")] #[deprecated(since = "1.0", - reason = "replaced with more ergonomic `default` and `default_with`")] + reason = "replaced with more ergonomic `or_insert` and `or_insert_with`")] + /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> { match self { Occupied(entry) => Ok(entry.into_mut()), @@ -1501,7 +1502,7 @@ impl<'a, K, V> Entry<'a, K, V> { reason = "matches entry v3 specification, waiting for dust to settle")] /// Ensures a value is in the entry by inserting the default if empty, and returns /// a mutable reference to the value in the entry. - pub fn default(self, default: V) -> &'a mut V { + pub fn or_insert(self, default: V) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), Vacant(entry) => entry.insert(default), @@ -1512,7 +1513,7 @@ impl<'a, K, V> Entry<'a, K, V> { reason = "matches entry v3 specification, waiting for dust to settle")] /// Ensures a value is in the entry by inserting the result of the default function if empty, /// and returns a mutable reference to the value in the entry. - pub fn default_with V>(self, default: F) -> &'a mut V { + pub fn or_insert_with V>(self, default: F) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), Vacant(entry) => entry.insert(default()), diff --git a/src/libstd/collections/mod.rs b/src/libstd/collections/mod.rs index b51948c160b..0ac97b71298 100644 --- a/src/libstd/collections/mod.rs +++ b/src/libstd/collections/mod.rs @@ -307,7 +307,7 @@ //! let message = "she sells sea shells by the sea shore"; //! //! for c in message.chars() { -//! *count.entry(c).default(0) += 1; +//! *count.entry(c).or_insert(0) += 1; //! } //! //! assert_eq!(count.get(&'s'), Some(&8)); @@ -340,7 +340,7 @@ //! for id in orders.into_iter() { //! // If this is the first time we've seen this customer, initialize them //! // with no blood alcohol. Otherwise, just retrieve them. -//! let person = blood_alcohol.entry(id).default(Person{id: id, blood_alcohol: 0.0}); +//! let person = blood_alcohol.entry(id).or_insert(Person{id: id, blood_alcohol: 0.0}); //! //! // Reduce their blood alcohol level. It takes time to order and drink a beer! //! person.blood_alcohol *= 0.9; -- cgit 1.4.1-3-g733a5