@@ -1336,6 +1336,10 @@ impl<'a, K, V> InternalEntry<K, V, &'a mut RawTable<K, V>> {
1336
1336
}
1337
1337
1338
1338
/// 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
1339
1343
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1340
1344
pub enum Entry < ' a , K : ' a , V : ' a > {
1341
1345
/// An occupied Entry.
@@ -1366,6 +1370,9 @@ impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for Entry<'a, K, V> {
1366
1370
}
1367
1371
1368
1372
/// A view into a single occupied location in a HashMap.
1373
+ /// It is part of the [`Entry`] enum.
1374
+ ///
1375
+ /// [`Entry`]: enum.Entry.html
1369
1376
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1370
1377
pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
1371
1378
key : Option < K > ,
@@ -1383,6 +1390,9 @@ impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for OccupiedEntry<'a, K, V> {
1383
1390
}
1384
1391
1385
1392
/// A view into a single empty location in a HashMap.
1393
+ /// It is part of the [`Entry`] enum.
1394
+ ///
1395
+ /// [`Entry`]: enum.Entry.html
1386
1396
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1387
1397
pub struct VacantEntry < ' a , K : ' a , V : ' a > {
1388
1398
hash : SafeHash ,
@@ -1551,6 +1561,20 @@ impl<'a, K, V> Entry<'a, K, V> {
1551
1561
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1552
1562
/// Ensures a value is in the entry by inserting the default if empty, and returns
1553
1563
/// 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
+ /// ```
1554
1578
pub fn or_insert ( self , default : V ) -> & ' a mut V {
1555
1579
match self {
1556
1580
Occupied ( entry) => entry. into_mut ( ) ,
@@ -1561,6 +1585,19 @@ impl<'a, K, V> Entry<'a, K, V> {
1561
1585
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1562
1586
/// Ensures a value is in the entry by inserting the result of the default function if empty,
1563
1587
/// 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
+ /// ```
1564
1601
pub fn or_insert_with < F : FnOnce ( ) -> V > ( self , default : F ) -> & ' a mut V {
1565
1602
match self {
1566
1603
Occupied ( entry) => entry. into_mut ( ) ,
@@ -1569,6 +1606,15 @@ impl<'a, K, V> Entry<'a, K, V> {
1569
1606
}
1570
1607
1571
1608
/// 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
+ /// ```
1572
1618
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1573
1619
pub fn key ( & self ) -> & K {
1574
1620
match * self {
@@ -1580,45 +1626,154 @@ impl<'a, K, V> Entry<'a, K, V> {
1580
1626
1581
1627
impl < ' a , K , V > OccupiedEntry < ' a , K , V > {
1582
1628
/// 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
+ /// ```
1583
1639
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1584
1640
pub fn key ( & self ) -> & K {
1585
1641
self . elem . read ( ) . 0
1586
1642
}
1587
1643
1588
1644
/// 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
+ /// ```
1589
1664
#[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
1590
1665
pub fn remove_pair ( self ) -> ( K , V ) {
1591
1666
pop_internal ( self . elem )
1592
1667
}
1593
1668
1594
1669
/// 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
+ /// ```
1595
1684
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1596
1685
pub fn get ( & self ) -> & V {
1597
1686
self . elem . read ( ) . 1
1598
1687
}
1599
1688
1600
1689
/// 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
+ /// ```
1601
1707
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1602
1708
pub fn get_mut ( & mut self ) -> & mut V {
1603
1709
self . elem . read_mut ( ) . 1
1604
1710
}
1605
1711
1606
1712
/// 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
+ /// ```
1608
1731
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1609
1732
pub fn into_mut ( self ) -> & ' a mut V {
1610
1733
self . elem . into_mut_refs ( ) . 1
1611
1734
}
1612
1735
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
+ /// ```
1614
1753
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1615
1754
pub fn insert ( & mut self , mut value : V ) -> V {
1616
1755
let old_value = self . get_mut ( ) ;
1617
1756
mem:: swap ( & mut value, old_value) ;
1618
1757
value
1619
1758
}
1620
1759
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
+ /// ```
1622
1777
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1623
1778
pub fn remove ( self ) -> V {
1624
1779
pop_internal ( self . elem ) . 1
@@ -1634,20 +1789,58 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
1634
1789
1635
1790
impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
1636
1791
/// 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
+ /// ```
1638
1802
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1639
1803
pub fn key ( & self ) -> & K {
1640
1804
& self . key
1641
1805
}
1642
1806
1643
1807
/// 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
+ /// ```
1644
1823
#[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
1645
1824
pub fn into_key ( self ) -> K {
1646
1825
self . key
1647
1826
}
1648
1827
1649
1828
/// 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
+ /// ```
1651
1844
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1652
1845
pub fn insert ( self , value : V ) -> & ' a mut V {
1653
1846
match self . elem {
0 commit comments