@@ -1143,15 +1143,39 @@ impl<'a, K, V> DoubleEndedIterator for RangeMut<'a, K, V> {
1143
1143
}
1144
1144
1145
1145
impl < ' a , K : Ord , V > Entry < ' a , K , V > {
1146
- /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
1147
1146
#[ unstable( feature = "std_misc" ,
1148
1147
reason = "will soon be replaced by or_insert" ) ]
1148
+ #[ deprecated( since = "1.0" ,
1149
+ reason = "replaced with more ergonomic `or_insert` and `or_insert_with`" ) ]
1150
+ /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
1149
1151
pub fn get ( self ) -> Result < & ' a mut V , VacantEntry < ' a , K , V > > {
1150
1152
match self {
1151
1153
Occupied ( entry) => Ok ( entry. into_mut ( ) ) ,
1152
1154
Vacant ( entry) => Err ( entry) ,
1153
1155
}
1154
1156
}
1157
+
1158
+ #[ unstable( feature = "collections" ,
1159
+ reason = "matches entry v3 specification, waiting for dust to settle" ) ]
1160
+ /// Ensures a value is in the entry by inserting the default if empty, and returns
1161
+ /// a mutable reference to the value in the entry.
1162
+ pub fn or_insert ( self , default : V ) -> & ' a mut V {
1163
+ match self {
1164
+ Occupied ( entry) => entry. into_mut ( ) ,
1165
+ Vacant ( entry) => entry. insert ( default) ,
1166
+ }
1167
+ }
1168
+
1169
+ #[ unstable( feature = "collections" ,
1170
+ reason = "matches entry v3 specification, waiting for dust to settle" ) ]
1171
+ /// Ensures a value is in the entry by inserting the result of the default function if empty,
1172
+ /// and returns a mutable reference to the value in the entry.
1173
+ pub fn or_insert_with < F : FnOnce ( ) -> V > ( self , default : F ) -> & ' a mut V {
1174
+ match self {
1175
+ Occupied ( entry) => entry. into_mut ( ) ,
1176
+ Vacant ( entry) => entry. insert ( default ( ) ) ,
1177
+ }
1178
+ }
1155
1179
}
1156
1180
1157
1181
impl < ' a , K : Ord , V > VacantEntry < ' a , K , V > {
@@ -1563,21 +1587,12 @@ impl<K: Ord, V> BTreeMap<K, V> {
1563
1587
/// ```
1564
1588
/// # #![feature(collections)]
1565
1589
/// use std::collections::BTreeMap;
1566
- /// use std::collections::btree_map::Entry;
1567
1590
///
1568
1591
/// let mut count: BTreeMap<&str, usize> = BTreeMap::new();
1569
1592
///
1570
1593
/// // count the number of occurrences of letters in the vec
1571
- /// for x in vec!["a","b","a","c","a","b"].iter() {
1572
- /// match count.entry(*x) {
1573
- /// Entry::Vacant(view) => {
1574
- /// view.insert(1);
1575
- /// },
1576
- /// Entry::Occupied(mut view) => {
1577
- /// let v = view.get_mut();
1578
- /// *v += 1;
1579
- /// },
1580
- /// }
1594
+ /// for x in vec!["a","b","a","c","a","b"] {
1595
+ /// *count.entry(x).or_insert(0) += 1;
1581
1596
/// }
1582
1597
///
1583
1598
/// assert_eq!(count["a"], 3);
0 commit comments