@@ -4070,6 +4070,43 @@ impl<K, V, S, A: Allocator + Clone> Debug for RawEntryBuilder<'_, K, V, S, A> {
4070
4070
///
4071
4071
/// [`HashMap`]: struct.HashMap.html
4072
4072
/// [`entry`]: struct.HashMap.html#method.entry
4073
+ ///
4074
+ /// # Examples
4075
+ ///
4076
+ /// ```
4077
+ /// use hashbrown::hash_map::{HashMap, Entry, OccupiedEntry};
4078
+ ///
4079
+ /// let mut map = HashMap::new();
4080
+ /// map.extend([('a', 1), ('b', 2), ('c', 3)]);
4081
+ /// assert_eq!(map.len(), 3);
4082
+ ///
4083
+ /// // Existing key (insert)
4084
+ /// let entry: Entry<_, _, _> = map.entry('a');
4085
+ /// let _raw_o: OccupiedEntry<_, _, _> = entry.insert(10);
4086
+ /// assert_eq!(map.len(), 3);
4087
+ /// // Nonexistent key (insert)
4088
+ /// map.entry('d').insert(40);
4089
+ ///
4090
+ /// // Existing key (or_insert)
4091
+ /// let v = map.entry('b').or_insert(20);
4092
+ /// assert_eq!(std::mem::replace(v, 20), 2);
4093
+ /// // Nonexistent key (or_insert)
4094
+ /// map.entry('e').or_insert(50);
4095
+ ///
4096
+ /// // Existing key (or_insert_with)
4097
+ /// let v = map.entry('c').or_insert_with(|| 30);
4098
+ /// assert_eq!(std::mem::replace(v, 30), 3);
4099
+ /// // Nonexistent key (or_insert_with)
4100
+ /// map.entry('f').or_insert_with(|| 60);
4101
+ ///
4102
+ /// println!("Our HashMap: {:?}", map);
4103
+ ///
4104
+ /// let mut vec: Vec<_> = map.iter().map(|(&k, &v)| (k, v)).collect();
4105
+ /// // The `Iter` iterator produces items in arbitrary order, so the
4106
+ /// // items must be sorted to test them against a sorted array.
4107
+ /// vec.sort_unstable();
4108
+ /// assert_eq!(vec, [('a', 10), ('b', 20), ('c', 30), ('d', 40), ('e', 50), ('f', 60)]);
4109
+ /// ```
4073
4110
pub enum Entry < ' a , K , V , S , A = Global >
4074
4111
where
4075
4112
A : Allocator + Clone ,
@@ -5908,6 +5945,41 @@ where
5908
5945
S : BuildHasher ,
5909
5946
A : Allocator + Clone ,
5910
5947
{
5948
+ /// Inserts all new key-values from the iterator to existing `HashMap<K, V, S, A>`.
5949
+ /// Replace values with existing keys with new values returned from the iterator.
5950
+ ///
5951
+ /// # Examples
5952
+ ///
5953
+ /// ```
5954
+ /// use hashbrown::hash_map::HashMap;
5955
+ ///
5956
+ /// let mut map = HashMap::new();
5957
+ /// map.insert(1, 100);
5958
+ ///
5959
+ /// let some_iter = [(1, 1), (2, 2)].into_iter();
5960
+ /// map.extend(some_iter);
5961
+ /// // Replace values with existing keys with new values returned from the iterator.
5962
+ /// // So that the map.get_key1(&1) doesn't return Some(&100).
5963
+ /// assert_eq!(map.get(&1), Some(&1));
5964
+ ///
5965
+ /// let some_vec: Vec<_> = vec![(3, 3), (4, 4)];
5966
+ /// map.extend(some_vec);
5967
+ ///
5968
+ /// let some_arr = [(5, 5), (6, 6)];
5969
+ /// map.extend(some_arr);
5970
+ /// let old_map_len = map.len();
5971
+ ///
5972
+ /// // You can also extend from another HashMap
5973
+ /// let mut new_map = HashMap::new();
5974
+ /// new_map.extend(map);
5975
+ /// assert_eq!(new_map.len(), old_map_len);
5976
+ ///
5977
+ /// let mut vec: Vec<_> = new_map.into_iter().collect();
5978
+ /// // The `IntoIter` iterator produces items in arbitrary order, so the
5979
+ /// // items must be sorted to test them against a sorted array.
5980
+ /// vec.sort_unstable();
5981
+ /// assert_eq!(vec, [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]);
5982
+ /// ```
5911
5983
#[ cfg_attr( feature = "inline-more" , inline) ]
5912
5984
fn extend < T : IntoIterator < Item = ( K , V ) > > ( & mut self , iter : T ) {
5913
5985
// Keys may be already present or show multiple times in the iterator.
@@ -5948,13 +6020,53 @@ where
5948
6020
}
5949
6021
}
5950
6022
6023
+ /// Inserts all new key-values from the iterator and replaces values with existing
6024
+ /// keys with new values returned from the iterator.
5951
6025
impl < ' a , K , V , S , A > Extend < ( & ' a K , & ' a V ) > for HashMap < K , V , S , A >
5952
6026
where
5953
6027
K : Eq + Hash + Copy ,
5954
6028
V : Copy ,
5955
6029
S : BuildHasher ,
5956
6030
A : Allocator + Clone ,
5957
6031
{
6032
+ /// Inserts all new key-values from the iterator to existing `HashMap<K, V, S, A>`.
6033
+ /// Replace values with existing keys with new values returned from the iterator.
6034
+ /// The keys and values must implement [`Copy`] trait.
6035
+ ///
6036
+ /// [`Copy`]: https://doc.rust-lang.org/core/marker/trait.Copy.html
6037
+ ///
6038
+ /// # Examples
6039
+ ///
6040
+ /// ```
6041
+ /// use hashbrown::hash_map::HashMap;
6042
+ ///
6043
+ /// let mut map = HashMap::new();
6044
+ /// map.insert(1, 100);
6045
+ ///
6046
+ /// let arr = [(1, 1), (2, 2)];
6047
+ /// let some_iter = arr.iter().map(|&(k, v)| (k, v));
6048
+ /// map.extend(some_iter);
6049
+ /// // Replace values with existing keys with new values returned from the iterator.
6050
+ /// // So that the map.get_key1(&1) doesn't return Some(&100).
6051
+ /// assert_eq!(map.get(&1), Some(&1));
6052
+ ///
6053
+ /// let some_vec: Vec<_> = vec![(3, 3), (4, 4)];
6054
+ /// map.extend(some_vec.iter().map(|&(k, v)| (k, v)));
6055
+ ///
6056
+ /// let some_arr = [(5, 5), (6, 6)];
6057
+ /// map.extend(some_arr.iter().map(|&(k, v)| (k, v)));
6058
+ ///
6059
+ /// // You can also extend from another HashMap
6060
+ /// let mut new_map = HashMap::new();
6061
+ /// new_map.extend(&map);
6062
+ /// assert_eq!(new_map, map);
6063
+ ///
6064
+ /// let mut vec: Vec<_> = new_map.into_iter().collect();
6065
+ /// // The `IntoIter` iterator produces items in arbitrary order, so the
6066
+ /// // items must be sorted to test them against a sorted array.
6067
+ /// vec.sort_unstable();
6068
+ /// assert_eq!(vec, [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]);
6069
+ /// ```
5958
6070
#[ cfg_attr( feature = "inline-more" , inline) ]
5959
6071
fn extend < T : IntoIterator < Item = ( & ' a K , & ' a V ) > > ( & mut self , iter : T ) {
5960
6072
self . extend ( iter. into_iter ( ) . map ( |( & key, & value) | ( key, value) ) ) ;
0 commit comments