Skip to content

Commit 69d282a

Browse files
authored
Rollup merge of rust-lang#34935 - GuillaumeGomez:hash_map_doc, r=steveklabnik
Add HashMap Entry enums examples Part of rust-lang#29348. r? @steveklabnik
2 parents a56741f + ec33dab commit 69d282a

File tree

1 file changed

+198
-5
lines changed
  • src/libstd/collections/hash

1 file changed

+198
-5
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 198 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,10 @@ impl<'a, K, V> InternalEntry<K, V, &'a mut RawTable<K, V>> {
13361336
}
13371337

13381338
/// A view into a single location in a map, which may be vacant or occupied.
1339+
/// This enum is constructed from the [`entry`] method on [`HashMap`].
1340+
///
1341+
/// [`HashMap`]: struct.HashMap.html
1342+
/// [`entry`]: struct.HashMap.html#method.entry
13391343
#[stable(feature = "rust1", since = "1.0.0")]
13401344
pub enum Entry<'a, K: 'a, V: 'a> {
13411345
/// An occupied Entry.
@@ -1366,6 +1370,9 @@ impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for Entry<'a, K, V> {
13661370
}
13671371

13681372
/// A view into a single occupied location in a HashMap.
1373+
/// It is part of the [`Entry`] enum.
1374+
///
1375+
/// [`Entry`]: enum.Entry.html
13691376
#[stable(feature = "rust1", since = "1.0.0")]
13701377
pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
13711378
key: Option<K>,
@@ -1383,6 +1390,9 @@ impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for OccupiedEntry<'a, K, V> {
13831390
}
13841391

13851392
/// A view into a single empty location in a HashMap.
1393+
/// It is part of the [`Entry`] enum.
1394+
///
1395+
/// [`Entry`]: enum.Entry.html
13861396
#[stable(feature = "rust1", since = "1.0.0")]
13871397
pub struct VacantEntry<'a, K: 'a, V: 'a> {
13881398
hash: SafeHash,
@@ -1551,6 +1561,20 @@ impl<'a, K, V> Entry<'a, K, V> {
15511561
#[stable(feature = "rust1", since = "1.0.0")]
15521562
/// Ensures a value is in the entry by inserting the default if empty, and returns
15531563
/// a mutable reference to the value in the entry.
1564+
///
1565+
/// # Examples
1566+
///
1567+
/// ```
1568+
/// use std::collections::HashMap;
1569+
///
1570+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1571+
/// map.entry("poneyland").or_insert(12);
1572+
///
1573+
/// assert_eq!(map["poneyland"], 12);
1574+
///
1575+
/// *map.entry("poneyland").or_insert(12) += 10;
1576+
/// assert_eq!(map["poneyland"], 22);
1577+
/// ```
15541578
pub fn or_insert(self, default: V) -> &'a mut V {
15551579
match self {
15561580
Occupied(entry) => entry.into_mut(),
@@ -1561,6 +1585,19 @@ impl<'a, K, V> Entry<'a, K, V> {
15611585
#[stable(feature = "rust1", since = "1.0.0")]
15621586
/// Ensures a value is in the entry by inserting the result of the default function if empty,
15631587
/// and returns a mutable reference to the value in the entry.
1588+
///
1589+
/// # Examples
1590+
///
1591+
/// ```
1592+
/// use std::collections::HashMap;
1593+
///
1594+
/// let mut map: HashMap<&str, String> = HashMap::new();
1595+
/// let s = "hoho".to_owned();
1596+
///
1597+
/// map.entry("poneyland").or_insert_with(|| s);
1598+
///
1599+
/// assert_eq!(map["poneyland"], "hoho".to_owned());
1600+
/// ```
15641601
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
15651602
match self {
15661603
Occupied(entry) => entry.into_mut(),
@@ -1569,6 +1606,15 @@ impl<'a, K, V> Entry<'a, K, V> {
15691606
}
15701607

15711608
/// Returns a reference to this entry's key.
1609+
///
1610+
/// # Examples
1611+
///
1612+
/// ```
1613+
/// use std::collections::HashMap;
1614+
///
1615+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1616+
/// assert_eq!(map.entry("poneyland").key(), &"poneyland");
1617+
/// ```
15721618
#[stable(feature = "map_entry_keys", since = "1.10.0")]
15731619
pub fn key(&self) -> &K {
15741620
match *self {
@@ -1580,45 +1626,154 @@ impl<'a, K, V> Entry<'a, K, V> {
15801626

15811627
impl<'a, K, V> OccupiedEntry<'a, K, V> {
15821628
/// Gets a reference to the key in the entry.
1629+
///
1630+
/// # Examples
1631+
///
1632+
/// ```
1633+
/// use std::collections::HashMap;
1634+
///
1635+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1636+
/// map.entry("poneyland").or_insert(12);
1637+
/// assert_eq!(map.entry("poneyland").key(), &"poneyland");
1638+
/// ```
15831639
#[stable(feature = "map_entry_keys", since = "1.10.0")]
15841640
pub fn key(&self) -> &K {
15851641
self.elem.read().0
15861642
}
15871643

15881644
/// Take the ownership of the key and value from the map.
1645+
///
1646+
/// # Examples
1647+
///
1648+
/// ```
1649+
/// #![feature(map_entry_recover_keys)]
1650+
///
1651+
/// use std::collections::HashMap;
1652+
/// use std::collections::hash_map::Entry;
1653+
///
1654+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1655+
/// map.entry("poneyland").or_insert(12);
1656+
///
1657+
/// if let Entry::Occupied(o) = map.entry("poneyland") {
1658+
/// // We delete the entry from the map.
1659+
/// o.remove_pair();
1660+
/// }
1661+
///
1662+
/// assert_eq!(map.contains_key("poneyland"), false);
1663+
/// ```
15891664
#[unstable(feature = "map_entry_recover_keys", issue = "34285")]
15901665
pub fn remove_pair(self) -> (K, V) {
15911666
pop_internal(self.elem)
15921667
}
15931668

15941669
/// Gets a reference to the value in the entry.
1670+
///
1671+
/// # Examples
1672+
///
1673+
/// ```
1674+
/// use std::collections::HashMap;
1675+
/// use std::collections::hash_map::Entry;
1676+
///
1677+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1678+
/// map.entry("poneyland").or_insert(12);
1679+
///
1680+
/// if let Entry::Occupied(o) = map.entry("poneyland") {
1681+
/// assert_eq!(o.get(), &12);
1682+
/// }
1683+
/// ```
15951684
#[stable(feature = "rust1", since = "1.0.0")]
15961685
pub fn get(&self) -> &V {
15971686
self.elem.read().1
15981687
}
15991688

16001689
/// Gets a mutable reference to the value in the entry.
1690+
///
1691+
/// # Examples
1692+
///
1693+
/// ```
1694+
/// use std::collections::HashMap;
1695+
/// use std::collections::hash_map::Entry;
1696+
///
1697+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1698+
/// map.entry("poneyland").or_insert(12);
1699+
///
1700+
/// assert_eq!(map["poneyland"], 12);
1701+
/// if let Entry::Occupied(mut o) = map.entry("poneyland") {
1702+
/// *o.get_mut() += 10;
1703+
/// }
1704+
///
1705+
/// assert_eq!(map["poneyland"], 22);
1706+
/// ```
16011707
#[stable(feature = "rust1", since = "1.0.0")]
16021708
pub fn get_mut(&mut self) -> &mut V {
16031709
self.elem.read_mut().1
16041710
}
16051711

16061712
/// Converts the OccupiedEntry into a mutable reference to the value in the entry
1607-
/// with a lifetime bound to the map itself
1713+
/// with a lifetime bound to the map itself.
1714+
///
1715+
/// # Examples
1716+
///
1717+
/// ```
1718+
/// use std::collections::HashMap;
1719+
/// use std::collections::hash_map::Entry;
1720+
///
1721+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1722+
/// map.entry("poneyland").or_insert(12);
1723+
///
1724+
/// assert_eq!(map["poneyland"], 12);
1725+
/// if let Entry::Occupied(o) = map.entry("poneyland") {
1726+
/// *o.into_mut() += 10;
1727+
/// }
1728+
///
1729+
/// assert_eq!(map["poneyland"], 22);
1730+
/// ```
16081731
#[stable(feature = "rust1", since = "1.0.0")]
16091732
pub fn into_mut(self) -> &'a mut V {
16101733
self.elem.into_mut_refs().1
16111734
}
16121735

1613-
/// Sets the value of the entry, and returns the entry's old value
1736+
/// Sets the value of the entry, and returns the entry's old value.
1737+
///
1738+
/// # Examples
1739+
///
1740+
/// ```
1741+
/// use std::collections::HashMap;
1742+
/// use std::collections::hash_map::Entry;
1743+
///
1744+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1745+
/// map.entry("poneyland").or_insert(12);
1746+
///
1747+
/// if let Entry::Occupied(mut o) = map.entry("poneyland") {
1748+
/// assert_eq!(o.insert(15), 12);
1749+
/// }
1750+
///
1751+
/// assert_eq!(map["poneyland"], 15);
1752+
/// ```
16141753
#[stable(feature = "rust1", since = "1.0.0")]
16151754
pub fn insert(&mut self, mut value: V) -> V {
16161755
let old_value = self.get_mut();
16171756
mem::swap(&mut value, old_value);
16181757
value
16191758
}
16201759

1621-
/// Takes the value out of the entry, and returns it
1760+
/// Takes the value out of the entry, and returns it.
1761+
///
1762+
/// # Examples
1763+
///
1764+
/// ```
1765+
/// use std::collections::HashMap;
1766+
/// use std::collections::hash_map::Entry;
1767+
///
1768+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1769+
/// map.entry("poneyland").or_insert(12);
1770+
///
1771+
/// if let Entry::Occupied(o) = map.entry("poneyland") {
1772+
/// assert_eq!(o.remove(), 12);
1773+
/// }
1774+
///
1775+
/// assert_eq!(map.contains_key("poneyland"), false);
1776+
/// ```
16221777
#[stable(feature = "rust1", since = "1.0.0")]
16231778
pub fn remove(self) -> V {
16241779
pop_internal(self.elem).1
@@ -1634,20 +1789,58 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
16341789

16351790
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
16361791
/// Gets a reference to the key that would be used when inserting a value
1637-
/// through the VacantEntry.
1792+
/// through the `VacantEntry`.
1793+
///
1794+
/// # Examples
1795+
///
1796+
/// ```
1797+
/// use std::collections::HashMap;
1798+
///
1799+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1800+
/// assert_eq!(map.entry("poneyland").key(), &"poneyland");
1801+
/// ```
16381802
#[stable(feature = "map_entry_keys", since = "1.10.0")]
16391803
pub fn key(&self) -> &K {
16401804
&self.key
16411805
}
16421806

16431807
/// Take ownership of the key.
1808+
///
1809+
/// # Examples
1810+
///
1811+
/// ```
1812+
/// #![feature(map_entry_recover_keys)]
1813+
///
1814+
/// use std::collections::HashMap;
1815+
/// use std::collections::hash_map::Entry;
1816+
///
1817+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1818+
///
1819+
/// if let Entry::Vacant(v) = map.entry("poneyland") {
1820+
/// v.into_key();
1821+
/// }
1822+
/// ```
16441823
#[unstable(feature = "map_entry_recover_keys", issue = "34285")]
16451824
pub fn into_key(self) -> K {
16461825
self.key
16471826
}
16481827

16491828
/// Sets the value of the entry with the VacantEntry's key,
1650-
/// and returns a mutable reference to it
1829+
/// and returns a mutable reference to it.
1830+
///
1831+
/// # Examples
1832+
///
1833+
/// ```
1834+
/// use std::collections::HashMap;
1835+
/// use std::collections::hash_map::Entry;
1836+
///
1837+
/// let mut map: HashMap<&str, u32> = HashMap::new();
1838+
///
1839+
/// if let Entry::Vacant(o) = map.entry("poneyland") {
1840+
/// o.insert(37);
1841+
/// }
1842+
/// assert_eq!(map["poneyland"], 37);
1843+
/// ```
16511844
#[stable(feature = "rust1", since = "1.0.0")]
16521845
pub fn insert(self, value: V) -> &'a mut V {
16531846
match self.elem {

0 commit comments

Comments
 (0)