@@ -3963,7 +3963,7 @@ impl<'a, K, V, S, A: Allocator + Clone> RawVacantEntryMut<'a, K, V, S, A> {
3963
3963
/// use core::hash::{BuildHasher, Hash};
3964
3964
/// use hashbrown::hash_map::{HashMap, RawEntryMut};
3965
3965
///
3966
- /// fn compute_hash <K, S>(hash_builder: &S) -> impl Fn(&K) -> u64 + '_
3966
+ /// fn make_hasher <K, S>(hash_builder: &S) -> impl Fn(&K) -> u64 + '_
3967
3967
/// where
3968
3968
/// K: Hash + ?Sized,
3969
3969
/// S: BuildHasher,
@@ -3979,12 +3979,12 @@ impl<'a, K, V, S, A: Allocator + Clone> RawVacantEntryMut<'a, K, V, S, A> {
3979
3979
/// let mut map: HashMap<&str, u32> = HashMap::new();
3980
3980
/// let key = "a";
3981
3981
/// let hash_builder = map.hasher().clone();
3982
- /// let hash = compute_hash (&hash_builder)(&key);
3982
+ /// let hash = make_hasher (&hash_builder)(&key);
3983
3983
///
3984
3984
/// match map.raw_entry_mut().from_hash(hash, |q| q == &key) {
3985
3985
/// RawEntryMut::Occupied(_) => panic!(),
3986
3986
/// RawEntryMut::Vacant(v) => assert_eq!(
3987
- /// v.insert_with_hasher(hash, key, 100, compute_hash (&hash_builder)),
3987
+ /// v.insert_with_hasher(hash, key, 100, make_hasher (&hash_builder)),
3988
3988
/// (&mut "a", &mut 100)
3989
3989
/// ),
3990
3990
/// }
@@ -4173,8 +4173,7 @@ impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for Entry<'_, K, V, S, A
4173
4173
/// Entry::Occupied(mut view) => {
4174
4174
/// assert_eq!(view.get(), &100);
4175
4175
/// let v = view.get_mut();
4176
- /// let new_v = (*v) * 10;
4177
- /// *v = new_v;
4176
+ /// *v *= 10;
4178
4177
/// assert_eq!(view.insert(1111), 1000);
4179
4178
/// }
4180
4179
/// }
@@ -4229,6 +4228,32 @@ impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for OccupiedEntry<'_, K,
4229
4228
/// It is part of the [`Entry`] enum.
4230
4229
///
4231
4230
/// [`Entry`]: enum.Entry.html
4231
+ ///
4232
+ /// # Examples
4233
+ ///
4234
+ /// ```
4235
+ /// use hashbrown::hash_map::{Entry, HashMap, VacantEntry};
4236
+ ///
4237
+ /// let mut map = HashMap::<&str, i32>::new();
4238
+ ///
4239
+ /// let entry_v: VacantEntry<_, _, _> = match map.entry("a") {
4240
+ /// Entry::Vacant(view) => view,
4241
+ /// Entry::Occupied(_) => unreachable!(),
4242
+ /// };
4243
+ /// entry_v.insert(10);
4244
+ /// assert!(map[&"a"] == 10 && map.len() == 1);
4245
+ ///
4246
+ /// // Nonexistent key (insert and update)
4247
+ /// match map.entry("b") {
4248
+ /// Entry::Occupied(_) => unreachable!(),
4249
+ /// Entry::Vacant(view) => {
4250
+ /// let value = view.insert(2);
4251
+ /// assert_eq!(*value, 2);
4252
+ /// *value = 20;
4253
+ /// }
4254
+ /// }
4255
+ /// assert!(map[&"b"] == 20 && map.len() == 2);
4256
+ /// ```
4232
4257
pub struct VacantEntry < ' a , K , V , S , A : Allocator + Clone = Global > {
4233
4258
hash : u64 ,
4234
4259
key : K ,
@@ -4241,12 +4266,21 @@ impl<K: Debug, V, S, A: Allocator + Clone> Debug for VacantEntry<'_, K, V, S, A>
4241
4266
}
4242
4267
}
4243
4268
4244
- /// A view into a single entry in a map, which may either be vacant or occupied.
4269
+ /// A view into a single entry in a map, which may either be vacant or occupied,
4270
+ /// with any borrowed form of the map's key type.
4271
+ ///
4245
4272
///
4246
4273
/// This `enum` is constructed from the [`entry_ref`] method on [`HashMap`].
4274
+ ///
4275
+ /// [`Hash`] and [`Eq`] on the borrowed form of the map's key type *must* match those
4276
+ /// for the key type. It also require that key may be constructed from the borrowed
4277
+ /// form through the [`From`] trait.
4247
4278
///
4248
4279
/// [`HashMap`]: struct.HashMap.html
4249
4280
/// [`entry_ref`]: struct.HashMap.html#method.entry_ref
4281
+ /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html
4282
+ /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html
4283
+ /// [`From`]: https://doc.rust-lang.org/std/convert/trait.From.html
4250
4284
///
4251
4285
/// # Examples
4252
4286
///
@@ -4360,6 +4394,43 @@ impl<'a, K: Borrow<Q>, Q: ?Sized> AsRef<Q> for KeyOrRef<'a, K, Q> {
4360
4394
/// It is part of the [`EntryRef`] enum.
4361
4395
///
4362
4396
/// [`EntryRef`]: enum.EntryRef.html
4397
+ ///
4398
+ /// # Examples
4399
+ ///
4400
+ /// ```
4401
+ /// use hashbrown::hash_map::{EntryRef, HashMap, OccupiedEntryRef};
4402
+ ///
4403
+ /// let mut map = HashMap::new();
4404
+ /// map.extend([("a".to_owned(), 10), ("b".into(), 20), ("c".into(), 30)]);
4405
+ ///
4406
+ /// let key = String::from("a");
4407
+ /// let _entry_o: OccupiedEntryRef<_, _, _, _> = map.entry_ref(&key).insert(100);
4408
+ /// assert_eq!(map.len(), 3);
4409
+ ///
4410
+ /// // Existing key (insert and update)
4411
+ /// match map.entry_ref("a") {
4412
+ /// EntryRef::Vacant(_) => unreachable!(),
4413
+ /// EntryRef::Occupied(mut view) => {
4414
+ /// assert_eq!(view.get(), &100);
4415
+ /// let v = view.get_mut();
4416
+ /// *v *= 10;
4417
+ /// assert_eq!(view.insert(1111), 1000);
4418
+ /// }
4419
+ /// }
4420
+ ///
4421
+ /// assert_eq!(map["a"], 1111);
4422
+ /// assert_eq!(map.len(), 3);
4423
+ ///
4424
+ /// // Existing key (take)
4425
+ /// match map.entry_ref("c") {
4426
+ /// EntryRef::Vacant(_) => unreachable!(),
4427
+ /// EntryRef::Occupied(view) => {
4428
+ /// assert_eq!(view.remove_entry(), ("c".to_owned(), 30));
4429
+ /// }
4430
+ /// }
4431
+ /// assert_eq!(map.get("c"), None);
4432
+ /// assert_eq!(map.len(), 2);
4433
+ /// ```
4363
4434
pub struct OccupiedEntryRef < ' a , ' b , K , Q : ?Sized , V , S , A : Allocator + Clone = Global > {
4364
4435
hash : u64 ,
4365
4436
key : Option < KeyOrRef < ' b , K , Q > > ,
@@ -4401,6 +4472,32 @@ impl<K: Borrow<Q>, Q: ?Sized + Debug, V: Debug, S, A: Allocator + Clone> Debug
4401
4472
/// It is part of the [`EntryRef`] enum.
4402
4473
///
4403
4474
/// [`EntryRef`]: enum.EntryRef.html
4475
+ ///
4476
+ /// # Examples
4477
+ ///
4478
+ /// ```
4479
+ /// use hashbrown::hash_map::{EntryRef, HashMap, VacantEntryRef};
4480
+ ///
4481
+ /// let mut map = HashMap::<String, i32>::new();
4482
+ ///
4483
+ /// let entry_v: VacantEntryRef<_, _, _, _> = match map.entry_ref("a") {
4484
+ /// EntryRef::Vacant(view) => view,
4485
+ /// EntryRef::Occupied(_) => unreachable!(),
4486
+ /// };
4487
+ /// entry_v.insert(10);
4488
+ /// assert!(map["a"] == 10 && map.len() == 1);
4489
+ ///
4490
+ /// // Nonexistent key (insert and update)
4491
+ /// match map.entry_ref("b") {
4492
+ /// EntryRef::Occupied(_) => unreachable!(),
4493
+ /// EntryRef::Vacant(view) => {
4494
+ /// let value = view.insert(2);
4495
+ /// assert_eq!(*value, 2);
4496
+ /// *value = 20;
4497
+ /// }
4498
+ /// }
4499
+ /// assert!(map["b"] == 20 && map.len() == 2);
4500
+ /// ```
4404
4501
pub struct VacantEntryRef < ' a , ' b , K , Q : ?Sized , V , S , A : Allocator + Clone = Global > {
4405
4502
hash : u64 ,
4406
4503
key : KeyOrRef < ' b , K , Q > ,
@@ -4418,6 +4515,27 @@ impl<K: Borrow<Q>, Q: ?Sized + Debug, V, S, A: Allocator + Clone> Debug
4418
4515
/// The error returned by [`try_insert`](HashMap::try_insert) when the key already exists.
4419
4516
///
4420
4517
/// Contains the occupied entry, and the value that was not inserted.
4518
+ ///
4519
+ /// # Examples
4520
+ ///
4521
+ /// ```
4522
+ /// use hashbrown::hash_map::{HashMap, OccupiedError};
4523
+ ///
4524
+ /// let mut map: HashMap<_, _> = [("a", 10), ("b", 20)].into();
4525
+ ///
4526
+ /// // try_insert method returns mutable reference to the value if keys are vacant,
4527
+ /// // but if the map did have key present, nothing is updated, and the provided
4528
+ /// // value is returned inside `Err(_)` variant
4529
+ /// match map.try_insert("a", 100) {
4530
+ /// Err(OccupiedError { mut entry, value }) => {
4531
+ /// assert_eq!(entry.key(), &"a");
4532
+ /// assert_eq!(value, 100);
4533
+ /// assert_eq!(entry.insert(100), 10)
4534
+ /// }
4535
+ /// _ => unreachable!(),
4536
+ /// }
4537
+ /// assert_eq!(map[&"a"], 100);
4538
+ /// ```
4421
4539
pub struct OccupiedError < ' a , K , V , S , A : Allocator + Clone = Global > {
4422
4540
/// The entry in the map that was already occupied.
4423
4541
pub entry : OccupiedEntry < ' a , K , V , S , A > ,
@@ -4453,6 +4571,28 @@ impl<'a, K, V, S, A: Allocator + Clone> IntoIterator for &'a HashMap<K, V, S, A>
4453
4571
type Item = ( & ' a K , & ' a V ) ;
4454
4572
type IntoIter = Iter < ' a , K , V > ;
4455
4573
4574
+ /// Creates an iterator over the entries of a `HashMap` in arbitrary order.
4575
+ /// The iterator element type is `(&'a K, &'a V)`.
4576
+ ///
4577
+ /// Return the same `Iter` struct as by the [`iter`] method on [`HashMap`].
4578
+ ///
4579
+ /// [`iter`]: struct.HashMap.html#method.iter
4580
+ /// [`HashMap`]: struct.HashMap.html
4581
+ ///
4582
+ /// # Examples
4583
+ ///
4584
+ /// ```
4585
+ /// use hashbrown::HashMap;
4586
+ /// let map_one: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into();
4587
+ /// let mut map_two = HashMap::new();
4588
+ ///
4589
+ /// for (key, value) in &map_one {
4590
+ /// println!("Key: {}, Value: {}", key, value);
4591
+ /// map_two.insert_unique_unchecked(*key, *value);
4592
+ /// }
4593
+ ///
4594
+ /// assert_eq!(map_one, map_two);
4595
+ /// ```
4456
4596
#[ cfg_attr( feature = "inline-more" , inline) ]
4457
4597
fn into_iter ( self ) -> Iter < ' a , K , V > {
4458
4598
self . iter ( )
@@ -4463,6 +4603,33 @@ impl<'a, K, V, S, A: Allocator + Clone> IntoIterator for &'a mut HashMap<K, V, S
4463
4603
type Item = ( & ' a K , & ' a mut V ) ;
4464
4604
type IntoIter = IterMut < ' a , K , V > ;
4465
4605
4606
+ /// Creates an iterator over the entries of a `HashMap` in arbitrary order
4607
+ /// with mutable references to the values. The iterator element type is
4608
+ /// `(&'a K, &'a mut V)`.
4609
+ ///
4610
+ /// Return the same `IterMut` struct as by the [`iter_mut`] method on
4611
+ /// [`HashMap`].
4612
+ ///
4613
+ /// [`iter_mut`]: struct.HashMap.html#method.iter_mut
4614
+ /// [`HashMap`]: struct.HashMap.html
4615
+ ///
4616
+ /// # Examples
4617
+ ///
4618
+ /// ```
4619
+ /// use hashbrown::HashMap;
4620
+ /// let mut map: HashMap<_, _> = [("a", 1), ("b", 2), ("c", 3)].into();
4621
+ ///
4622
+ /// for (key, value) in &mut map {
4623
+ /// println!("Key: {}, Value: {}", key, value);
4624
+ /// *value *= 2;
4625
+ /// }
4626
+ ///
4627
+ /// let mut vec = map.iter().collect::<Vec<_>>();
4628
+ /// // The `Iter` iterator produces items in arbitrary order, so the
4629
+ /// // items must be sorted to test them against a sorted array.
4630
+ /// vec.sort_unstable();
4631
+ /// assert_eq!(vec, [(&"a", &2), (&"b", &4), (&"c", &6)]);
4632
+ /// ```
4466
4633
#[ cfg_attr( feature = "inline-more" , inline) ]
4467
4634
fn into_iter ( self ) -> IterMut < ' a , K , V > {
4468
4635
self . iter_mut ( )
@@ -4482,13 +4649,14 @@ impl<K, V, S, A: Allocator + Clone> IntoIterator for HashMap<K, V, S, A> {
4482
4649
/// ```
4483
4650
/// use hashbrown::HashMap;
4484
4651
///
4485
- /// let mut map = HashMap::new();
4486
- /// map.insert("a", 1);
4487
- /// map.insert("b", 2);
4488
- /// map.insert("c", 3);
4652
+ /// let map: HashMap<_, _> = [("a", 1), ("b", 2), ("c", 3)].into();
4489
4653
///
4490
4654
/// // Not possible with .iter()
4491
- /// let vec: Vec<(&str, i32)> = map.into_iter().collect();
4655
+ /// let mut vec: Vec<(&str, i32)> = map.into_iter().collect();
4656
+ /// // The `IntoIter` iterator produces items in arbitrary order, so
4657
+ /// // the items must be sorted to test them against a sorted array.
4658
+ /// vec.sort_unstable();
4659
+ /// assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3)]);
4492
4660
/// ```
4493
4661
#[ cfg_attr( feature = "inline-more" , inline) ]
4494
4662
fn into_iter ( self ) -> IntoIter < K , V , A > {
0 commit comments