@@ -285,6 +285,12 @@ pub struct Values<'a, K: 'a, V: 'a> {
285
285
inner : Iter < ' a , K , V > ,
286
286
}
287
287
288
+ /// A mutable iterator over a BTreeMap's values.
289
+ #[ unstable( feature = "map_values_mut" , reason = "recently added" , issue = "32551" ) ]
290
+ pub struct ValuesMut < ' a , K : ' a , V : ' a > {
291
+ inner : IterMut < ' a , K , V > ,
292
+ }
293
+
288
294
/// An iterator over a sub-range of BTreeMap's entries.
289
295
pub struct Range < ' a , K : ' a , V : ' a > {
290
296
front : Handle < NodeRef < marker:: Immut < ' a > , K , V , marker:: Leaf > , marker:: Edge > ,
@@ -1006,6 +1012,33 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
1006
1012
}
1007
1013
}
1008
1014
1015
+ #[ unstable( feature = "map_values_mut" , reason = "recently added" , issue = "32551" ) ]
1016
+ impl < ' a , K , V > Iterator for ValuesMut < ' a , K , V > {
1017
+ type Item = & ' a mut V ;
1018
+
1019
+ fn next ( & mut self ) -> Option < & ' a mut V > {
1020
+ self . inner . next ( ) . map ( |( _, v) | v)
1021
+ }
1022
+
1023
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1024
+ self . inner . size_hint ( )
1025
+ }
1026
+ }
1027
+
1028
+ #[ unstable( feature = "map_values_mut" , reason = "recently added" , issue = "32551" ) ]
1029
+ impl < ' a , K , V > DoubleEndedIterator for ValuesMut < ' a , K , V > {
1030
+ fn next_back ( & mut self ) -> Option < & ' a mut V > {
1031
+ self . inner . next_back ( ) . map ( |( _, v) | v)
1032
+ }
1033
+ }
1034
+
1035
+ #[ unstable( feature = "map_values_mut" , reason = "recently added" , issue = "32551" ) ]
1036
+ impl < ' a , K , V > ExactSizeIterator for ValuesMut < ' a , K , V > {
1037
+ fn len ( & self ) -> usize {
1038
+ self . inner . len ( )
1039
+ }
1040
+ }
1041
+
1009
1042
impl < ' a , K , V > Range < ' a , K , V > {
1010
1043
unsafe fn next_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
1011
1044
let handle = self . front ;
@@ -1403,6 +1436,33 @@ impl<K, V> BTreeMap<K, V> {
1403
1436
Values { inner : self . iter ( ) }
1404
1437
}
1405
1438
1439
+ /// Gets a mutable iterator over the values of the map, in order by key.
1440
+ ///
1441
+ /// # Examples
1442
+ ///
1443
+ /// Basic usage:
1444
+ ///
1445
+ /// ```
1446
+ /// # #![feature(map_values_mut)]
1447
+ /// use std::collections::BTreeMap;
1448
+ ///
1449
+ /// let mut a = BTreeMap::new();
1450
+ /// a.insert(1, String::from("hello"));
1451
+ /// a.insert(2, String::from("goodbye"));
1452
+ ///
1453
+ /// for value in a.values_mut() {
1454
+ /// value.push_str("!");
1455
+ /// }
1456
+ ///
1457
+ /// let values: Vec<String> = a.values().cloned().collect();
1458
+ /// assert_eq!(values, [String::from("hello!"),
1459
+ /// String::from("goodbye!")]);
1460
+ /// ```
1461
+ #[ unstable( feature = "map_values_mut" , reason = "recently added" , issue = "32551" ) ]
1462
+ pub fn values_mut < ' a > ( & ' a mut self ) -> ValuesMut < ' a , K , V > {
1463
+ ValuesMut { inner : self . iter_mut ( ) }
1464
+ }
1465
+
1406
1466
/// Returns the number of elements in the map.
1407
1467
///
1408
1468
/// # Examples
0 commit comments