Skip to content

Commit 641c6da

Browse files
committed
Add doc for EntryRef and OccupiedEntry
1 parent 7aa113f commit 641c6da

File tree

1 file changed

+134
-12
lines changed

1 file changed

+134
-12
lines changed

src/map.rs

Lines changed: 134 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,47 +4074,71 @@ impl<K, V, S, A: Allocator + Clone> Debug for RawEntryBuilder<'_, K, V, S, A> {
40744074
/// # Examples
40754075
///
40764076
/// ```
4077-
/// use hashbrown::hash_map::{HashMap, Entry, OccupiedEntry};
4077+
/// use hashbrown::hash_map::{Entry, HashMap, OccupiedEntry};
40784078
///
40794079
/// let mut map = HashMap::new();
4080-
/// map.extend([('a', 1), ('b', 2), ('c', 3)]);
4080+
/// map.extend([("a", 10), ("b", 20), ("c", 30)]);
40814081
/// assert_eq!(map.len(), 3);
40824082
///
40834083
/// // 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);
40864086
/// assert_eq!(map.len(), 3);
40874087
/// // Nonexistent key (insert)
4088-
/// map.entry('d').insert(40);
4088+
/// map.entry("d").insert(4);
40894089
///
40904090
/// // 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);
40934093
/// // Nonexistent key (or_insert)
4094-
/// map.entry('e').or_insert(50);
4094+
/// map.entry("e").or_insert(5);
40954095
///
40964096
/// // 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);
40994099
/// // Nonexistent key (or_insert_with)
4100-
/// map.entry('f').or_insert_with(|| 60);
4100+
/// map.entry("f").or_insert_with(|| 6);
41014101
///
41024102
/// println!("Our HashMap: {:?}", map);
41034103
///
41044104
/// let mut vec: Vec<_> = map.iter().map(|(&k, &v)| (k, v)).collect();
41054105
/// // The `Iter` iterator produces items in arbitrary order, so the
41064106
/// // items must be sorted to test them against a sorted array.
41074107
/// 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)]);
41094109
/// ```
41104110
pub enum Entry<'a, K, V, S, A = Global>
41114111
where
41124112
A: Allocator + Clone,
41134113
{
41144114
/// 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+
/// ```
41154127
Occupied(OccupiedEntry<'a, K, V, S, A>),
41164128

41174129
/// 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+
/// ```
41184142
Vacant(VacantEntry<'a, K, V, S, A>),
41194143
}
41204144

@@ -4131,6 +4155,43 @@ impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for Entry<'_, K, V, S, A
41314155
/// It is part of the [`Entry`] enum.
41324156
///
41334157
/// [`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+
/// ```
41344195
pub struct OccupiedEntry<'a, K, V, S, A: Allocator + Clone = Global> {
41354196
hash: u64,
41364197
key: Option<K>,
@@ -4186,14 +4247,75 @@ impl<K: Debug, V, S, A: Allocator + Clone> Debug for VacantEntry<'_, K, V, S, A>
41864247
///
41874248
/// [`HashMap`]: struct.HashMap.html
41884249
/// [`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+
/// ```
41894287
pub enum EntryRef<'a, 'b, K, Q: ?Sized, V, S, A = Global>
41904288
where
41914289
A: Allocator + Clone,
41924290
{
41934291
/// 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+
/// ```
41944304
Occupied(OccupiedEntryRef<'a, 'b, K, Q, V, S, A>),
41954305

41964306
/// 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+
/// ```
41974319
Vacant(VacantEntryRef<'a, 'b, K, Q, V, S, A>),
41984320
}
41994321

0 commit comments

Comments
 (0)