@@ -4074,47 +4074,71 @@ impl<K, V, S, A: Allocator + Clone> Debug for RawEntryBuilder<'_, K, V, S, A> {
4074
4074
/// # Examples
4075
4075
///
4076
4076
/// ```
4077
- /// use hashbrown::hash_map::{HashMap, Entry , OccupiedEntry};
4077
+ /// use hashbrown::hash_map::{Entry, HashMap , OccupiedEntry};
4078
4078
///
4079
4079
/// let mut map = HashMap::new();
4080
- /// map.extend([('a', 1 ), ('b', 2 ), ('c', 3 )]);
4080
+ /// map.extend([("a", 10 ), ("b", 20 ), ("c", 30 )]);
4081
4081
/// assert_eq!(map.len(), 3);
4082
4082
///
4083
4083
/// // Existing key (insert)
4084
- /// let entry: Entry<_, _, _> = map.entry('a' );
4085
- /// let _raw_o: OccupiedEntry<_, _, _> = entry.insert(10 );
4084
+ /// let entry: Entry<_, _, _> = map.entry("a" );
4085
+ /// let _raw_o: OccupiedEntry<_, _, _> = entry.insert(1 );
4086
4086
/// assert_eq!(map.len(), 3);
4087
4087
/// // Nonexistent key (insert)
4088
- /// map.entry('d' ).insert(40 );
4088
+ /// map.entry("d" ).insert(4 );
4089
4089
///
4090
4090
/// // Existing key (or_insert)
4091
- /// let v = map.entry('b' ).or_insert(20 );
4092
- /// assert_eq!(std::mem::replace(v, 20 ), 2 );
4091
+ /// let v = map.entry("b" ).or_insert(2 );
4092
+ /// assert_eq!(std::mem::replace(v, 2 ), 20 );
4093
4093
/// // Nonexistent key (or_insert)
4094
- /// map.entry('e' ).or_insert(50 );
4094
+ /// map.entry("e" ).or_insert(5 );
4095
4095
///
4096
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 );
4097
+ /// let v = map.entry("c" ).or_insert_with(|| 3 );
4098
+ /// assert_eq!(std::mem::replace(v, 3 ), 30 );
4099
4099
/// // Nonexistent key (or_insert_with)
4100
- /// map.entry('f' ).or_insert_with(|| 60 );
4100
+ /// map.entry("f" ).or_insert_with(|| 6 );
4101
4101
///
4102
4102
/// println!("Our HashMap: {:?}", map);
4103
4103
///
4104
4104
/// let mut vec: Vec<_> = map.iter().map(|(&k, &v)| (k, v)).collect();
4105
4105
/// // The `Iter` iterator produces items in arbitrary order, so the
4106
4106
/// // items must be sorted to test them against a sorted array.
4107
4107
/// vec.sort_unstable();
4108
- /// assert_eq!(vec, [('a', 10 ), ('b', 20 ), ('c', 30 ), ('d', 40 ), ('e', 50 ), ('f', 60 )]);
4108
+ /// assert_eq!(vec, [("a", 1 ), ("b", 2 ), ("c", 3 ), ("d", 4 ), ("e", 5 ), ("f", 6 )]);
4109
4109
/// ```
4110
4110
pub enum Entry < ' a , K , V , S , A = Global >
4111
4111
where
4112
4112
A : Allocator + Clone ,
4113
4113
{
4114
4114
/// An occupied entry.
4115
+ ///
4116
+ /// # Examples
4117
+ ///
4118
+ /// ```
4119
+ /// use hashbrown::hash_map::{Entry, HashMap};
4120
+ /// let mut map: HashMap<_, _> = [("a", 100), ("b", 200)].into();
4121
+ ///
4122
+ /// match map.entry("a") {
4123
+ /// Entry::Vacant(_) => unreachable!(),
4124
+ /// Entry::Occupied(_) => { }
4125
+ /// }
4126
+ /// ```
4115
4127
Occupied ( OccupiedEntry < ' a , K , V , S , A > ) ,
4116
4128
4117
4129
/// A vacant entry.
4130
+ ///
4131
+ /// # Examples
4132
+ ///
4133
+ /// ```
4134
+ /// use hashbrown::hash_map::{Entry, HashMap};
4135
+ /// let mut map: HashMap<&str, i32> = HashMap::new();
4136
+ ///
4137
+ /// match map.entry("a") {
4138
+ /// Entry::Occupied(_) => unreachable!(),
4139
+ /// Entry::Vacant(_) => { }
4140
+ /// }
4141
+ /// ```
4118
4142
Vacant ( VacantEntry < ' a , K , V , S , A > ) ,
4119
4143
}
4120
4144
@@ -4131,6 +4155,43 @@ impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for Entry<'_, K, V, S, A
4131
4155
/// It is part of the [`Entry`] enum.
4132
4156
///
4133
4157
/// [`Entry`]: enum.Entry.html
4158
+ ///
4159
+ /// # Examples
4160
+ ///
4161
+ /// ```
4162
+ /// use hashbrown::hash_map::{Entry, HashMap, OccupiedEntry};
4163
+ ///
4164
+ /// let mut map = HashMap::new();
4165
+ /// map.extend([("a", 10), ("b", 20), ("c", 30)]);
4166
+ ///
4167
+ /// let _entry_o: OccupiedEntry<_, _, _> = map.entry("a").insert(100);
4168
+ /// assert_eq!(map.len(), 3);
4169
+ ///
4170
+ /// // Existing key (insert and update)
4171
+ /// match map.entry("a") {
4172
+ /// Entry::Vacant(_) => unreachable!(),
4173
+ /// Entry::Occupied(mut view) => {
4174
+ /// assert_eq!(view.get(), &100);
4175
+ /// let v = view.get_mut();
4176
+ /// let new_v = (*v) * 10;
4177
+ /// *v = new_v;
4178
+ /// assert_eq!(view.insert(1111), 1000);
4179
+ /// }
4180
+ /// }
4181
+ ///
4182
+ /// assert_eq!(map[&"a"], 1111);
4183
+ /// assert_eq!(map.len(), 3);
4184
+ ///
4185
+ /// // Existing key (take)
4186
+ /// match map.entry("c") {
4187
+ /// Entry::Vacant(_) => unreachable!(),
4188
+ /// Entry::Occupied(view) => {
4189
+ /// assert_eq!(view.remove_entry(), ("c", 30));
4190
+ /// }
4191
+ /// }
4192
+ /// assert_eq!(map.get(&"c"), None);
4193
+ /// assert_eq!(map.len(), 2);
4194
+ /// ```
4134
4195
pub struct OccupiedEntry < ' a , K , V , S , A : Allocator + Clone = Global > {
4135
4196
hash : u64 ,
4136
4197
key : Option < K > ,
@@ -4186,14 +4247,75 @@ impl<K: Debug, V, S, A: Allocator + Clone> Debug for VacantEntry<'_, K, V, S, A>
4186
4247
///
4187
4248
/// [`HashMap`]: struct.HashMap.html
4188
4249
/// [`entry_ref`]: struct.HashMap.html#method.entry_ref
4250
+ ///
4251
+ /// # Examples
4252
+ ///
4253
+ /// ```
4254
+ /// use hashbrown::hash_map::{EntryRef, HashMap, OccupiedEntryRef};
4255
+ ///
4256
+ /// let mut map = HashMap::new();
4257
+ /// map.extend([("a".to_owned(), 10), ("b".into(), 20), ("c".into(), 30)]);
4258
+ /// assert_eq!(map.len(), 3);
4259
+ ///
4260
+ /// // Existing key (insert)
4261
+ /// let key = String::from("a");
4262
+ /// let entry: EntryRef<_, _, _, _> = map.entry_ref(&key);
4263
+ /// let _raw_o: OccupiedEntryRef<_, _, _, _> = entry.insert(1);
4264
+ /// assert_eq!(map.len(), 3);
4265
+ /// // Nonexistent key (insert)
4266
+ /// map.entry_ref("d").insert(4);
4267
+ ///
4268
+ /// // Existing key (or_insert)
4269
+ /// let v = map.entry_ref("b").or_insert(2);
4270
+ /// assert_eq!(std::mem::replace(v, 2), 20);
4271
+ /// // Nonexistent key (or_insert)
4272
+ /// map.entry_ref("e").or_insert(5);
4273
+ ///
4274
+ /// // Existing key (or_insert_with)
4275
+ /// let v = map.entry_ref("c").or_insert_with(|| 3);
4276
+ /// assert_eq!(std::mem::replace(v, 3), 30);
4277
+ /// // Nonexistent key (or_insert_with)
4278
+ /// map.entry_ref("f").or_insert_with(|| 6);
4279
+ ///
4280
+ /// println!("Our HashMap: {:?}", map);
4281
+ ///
4282
+ /// for (key, value) in ["a", "b", "c", "d", "e", "f"].into_iter().zip(1..=6) {
4283
+ /// assert_eq!(map[key], value)
4284
+ /// }
4285
+ /// assert_eq!(map.len(), 6);
4286
+ /// ```
4189
4287
pub enum EntryRef < ' a , ' b , K , Q : ?Sized , V , S , A = Global >
4190
4288
where
4191
4289
A : Allocator + Clone ,
4192
4290
{
4193
4291
/// An occupied entry.
4292
+ ///
4293
+ /// # Examples
4294
+ ///
4295
+ /// ```
4296
+ /// use hashbrown::hash_map::{EntryRef, HashMap};
4297
+ /// let mut map: HashMap<_, _> = [("a".to_owned(), 100), ("b".into(), 200)].into();
4298
+ ///
4299
+ /// match map.entry_ref("a") {
4300
+ /// EntryRef::Vacant(_) => unreachable!(),
4301
+ /// EntryRef::Occupied(_) => { }
4302
+ /// }
4303
+ /// ```
4194
4304
Occupied ( OccupiedEntryRef < ' a , ' b , K , Q , V , S , A > ) ,
4195
4305
4196
4306
/// A vacant entry.
4307
+ ///
4308
+ /// # Examples
4309
+ ///
4310
+ /// ```
4311
+ /// use hashbrown::hash_map::{EntryRef, HashMap};
4312
+ /// let mut map: HashMap<String, i32> = HashMap::new();
4313
+ ///
4314
+ /// match map.entry_ref("a") {
4315
+ /// EntryRef::Occupied(_) => unreachable!(),
4316
+ /// EntryRef::Vacant(_) => { }
4317
+ /// }
4318
+ /// ```
4197
4319
Vacant ( VacantEntryRef < ' a , ' b , K , Q , V , S , A > ) ,
4198
4320
}
4199
4321
0 commit comments