@@ -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.
@@ -1352,13 +1356,19 @@ pub enum Entry<'a, K: 'a, V: 'a> {
1352
1356
}
1353
1357
1354
1358
/// A view into a single occupied location in a HashMap.
1359
+ /// It is part of the [`Entry`] enum.
1360
+ ///
1361
+ /// [`Entry`]: enum.Entry.html
1355
1362
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1356
1363
pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
1357
1364
key : Option < K > ,
1358
1365
elem : FullBucket < K , V , & ' a mut RawTable < K , V > > ,
1359
1366
}
1360
1367
1361
1368
/// A view into a single empty location in a HashMap.
1369
+ /// It is part of the [`Entry`] enum.
1370
+ ///
1371
+ /// [`Entry`]: enum.Entry.html
1362
1372
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1363
1373
pub struct VacantEntry < ' a , K : ' a , V : ' a > {
1364
1374
hash : SafeHash ,
@@ -1518,6 +1528,20 @@ impl<'a, K, V> Entry<'a, K, V> {
1518
1528
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1519
1529
/// Ensures a value is in the entry by inserting the default if empty, and returns
1520
1530
/// a mutable reference to the value in the entry.
1531
+ ///
1532
+ /// # Examples
1533
+ ///
1534
+ /// ```
1535
+ /// use std::collections::HashMap;
1536
+ ///
1537
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1538
+ /// map.entry("poneyland").or_insert(12);
1539
+ ///
1540
+ /// assert_eq!(map["poneyland"], 12);
1541
+ ///
1542
+ /// *map.entry("poneyland").or_insert(12) += 10;
1543
+ /// assert_eq!(map["poneyland"], 22);
1544
+ /// ```
1521
1545
pub fn or_insert ( self , default : V ) -> & ' a mut V {
1522
1546
match self {
1523
1547
Occupied ( entry) => entry. into_mut ( ) ,
@@ -1528,6 +1552,19 @@ impl<'a, K, V> Entry<'a, K, V> {
1528
1552
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1529
1553
/// Ensures a value is in the entry by inserting the result of the default function if empty,
1530
1554
/// and returns a mutable reference to the value in the entry.
1555
+ ///
1556
+ /// # Examples
1557
+ ///
1558
+ /// ```
1559
+ /// use std::collections::HashMap;
1560
+ ///
1561
+ /// let mut map: HashMap<&str, String> = HashMap::new();
1562
+ /// let s = "hoho".to_owned();
1563
+ ///
1564
+ /// map.entry("poneyland").or_insert_with(|| s);
1565
+ ///
1566
+ /// assert_eq!(map["poneyland"], "hoho".to_owned());
1567
+ /// ```
1531
1568
pub fn or_insert_with < F : FnOnce ( ) -> V > ( self , default : F ) -> & ' a mut V {
1532
1569
match self {
1533
1570
Occupied ( entry) => entry. into_mut ( ) ,
@@ -1536,6 +1573,15 @@ impl<'a, K, V> Entry<'a, K, V> {
1536
1573
}
1537
1574
1538
1575
/// Returns a reference to this entry's key.
1576
+ ///
1577
+ /// # Examples
1578
+ ///
1579
+ /// ```
1580
+ /// use std::collections::HashMap;
1581
+ ///
1582
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1583
+ /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
1584
+ /// ```
1539
1585
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1540
1586
pub fn key ( & self ) -> & K {
1541
1587
match * self {
@@ -1547,45 +1593,154 @@ impl<'a, K, V> Entry<'a, K, V> {
1547
1593
1548
1594
impl < ' a , K , V > OccupiedEntry < ' a , K , V > {
1549
1595
/// Gets a reference to the key in the entry.
1596
+ ///
1597
+ /// # Examples
1598
+ ///
1599
+ /// ```
1600
+ /// use std::collections::HashMap;
1601
+ ///
1602
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1603
+ /// map.entry("poneyland").or_insert(12);
1604
+ /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
1605
+ /// ```
1550
1606
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1551
1607
pub fn key ( & self ) -> & K {
1552
1608
self . elem . read ( ) . 0
1553
1609
}
1554
1610
1555
1611
/// Take the ownership of the key and value from the map.
1612
+ ///
1613
+ /// # Examples
1614
+ ///
1615
+ /// ```
1616
+ /// #![feature(map_entry_recover_keys)]
1617
+ ///
1618
+ /// use std::collections::HashMap;
1619
+ /// use std::collections::hash_map::Entry;
1620
+ ///
1621
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1622
+ /// map.entry("poneyland").or_insert(12);
1623
+ ///
1624
+ /// if let Entry::Occupied(o) = map.entry("poneyland") {
1625
+ /// // We delete the entry from the map.
1626
+ /// o.remove_pair();
1627
+ /// }
1628
+ ///
1629
+ /// assert_eq!(map.contains_key("poneyland"), false);
1630
+ /// ```
1556
1631
#[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
1557
1632
pub fn remove_pair ( self ) -> ( K , V ) {
1558
1633
pop_internal ( self . elem )
1559
1634
}
1560
1635
1561
1636
/// Gets a reference to the value in the entry.
1637
+ ///
1638
+ /// # Examples
1639
+ ///
1640
+ /// ```
1641
+ /// use std::collections::HashMap;
1642
+ /// use std::collections::hash_map::Entry;
1643
+ ///
1644
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1645
+ /// map.entry("poneyland").or_insert(12);
1646
+ ///
1647
+ /// if let Entry::Occupied(o) = map.entry("poneyland") {
1648
+ /// assert_eq!(o.get(), &12);
1649
+ /// }
1650
+ /// ```
1562
1651
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1563
1652
pub fn get ( & self ) -> & V {
1564
1653
self . elem . read ( ) . 1
1565
1654
}
1566
1655
1567
1656
/// Gets a mutable reference to the value in the entry.
1657
+ ///
1658
+ /// # Examples
1659
+ ///
1660
+ /// ```
1661
+ /// use std::collections::HashMap;
1662
+ /// use std::collections::hash_map::Entry;
1663
+ ///
1664
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1665
+ /// map.entry("poneyland").or_insert(12);
1666
+ ///
1667
+ /// assert_eq!(map["poneyland"], 12);
1668
+ /// if let Entry::Occupied(mut o) = map.entry("poneyland") {
1669
+ /// *o.get_mut() += 10;
1670
+ /// }
1671
+ ///
1672
+ /// assert_eq!(map["poneyland"], 22);
1673
+ /// ```
1568
1674
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1569
1675
pub fn get_mut ( & mut self ) -> & mut V {
1570
1676
self . elem . read_mut ( ) . 1
1571
1677
}
1572
1678
1573
1679
/// Converts the OccupiedEntry into a mutable reference to the value in the entry
1574
- /// with a lifetime bound to the map itself
1680
+ /// with a lifetime bound to the map itself.
1681
+ ///
1682
+ /// # Examples
1683
+ ///
1684
+ /// ```
1685
+ /// use std::collections::HashMap;
1686
+ /// use std::collections::hash_map::Entry;
1687
+ ///
1688
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1689
+ /// map.entry("poneyland").or_insert(12);
1690
+ ///
1691
+ /// assert_eq!(map["poneyland"], 12);
1692
+ /// if let Entry::Occupied(o) = map.entry("poneyland") {
1693
+ /// *o.into_mut() += 10;
1694
+ /// }
1695
+ ///
1696
+ /// assert_eq!(map["poneyland"], 22);
1697
+ /// ```
1575
1698
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1576
1699
pub fn into_mut ( self ) -> & ' a mut V {
1577
1700
self . elem . into_mut_refs ( ) . 1
1578
1701
}
1579
1702
1580
- /// Sets the value of the entry, and returns the entry's old value
1703
+ /// Sets the value of the entry, and returns the entry's old value.
1704
+ ///
1705
+ /// # Examples
1706
+ ///
1707
+ /// ```
1708
+ /// use std::collections::HashMap;
1709
+ /// use std::collections::hash_map::Entry;
1710
+ ///
1711
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1712
+ /// map.entry("poneyland").or_insert(12);
1713
+ ///
1714
+ /// if let Entry::Occupied(mut o) = map.entry("poneyland") {
1715
+ /// assert_eq!(o.insert(15), 12);
1716
+ /// }
1717
+ ///
1718
+ /// assert_eq!(map["poneyland"], 15);
1719
+ /// ```
1581
1720
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1582
1721
pub fn insert ( & mut self , mut value : V ) -> V {
1583
1722
let old_value = self . get_mut ( ) ;
1584
1723
mem:: swap ( & mut value, old_value) ;
1585
1724
value
1586
1725
}
1587
1726
1588
- /// Takes the value out of the entry, and returns it
1727
+ /// Takes the value out of the entry, and returns it.
1728
+ ///
1729
+ /// # Examples
1730
+ ///
1731
+ /// ```
1732
+ /// use std::collections::HashMap;
1733
+ /// use std::collections::hash_map::Entry;
1734
+ ///
1735
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1736
+ /// map.entry("poneyland").or_insert(12);
1737
+ ///
1738
+ /// if let Entry::Occupied(o) = map.entry("poneyland") {
1739
+ /// assert_eq!(o.remove(), 12);
1740
+ /// }
1741
+ ///
1742
+ /// assert_eq!(map.contains_key("poneyland"), false);
1743
+ /// ```
1589
1744
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1590
1745
pub fn remove ( self ) -> V {
1591
1746
pop_internal ( self . elem ) . 1
@@ -1601,20 +1756,58 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
1601
1756
1602
1757
impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
1603
1758
/// Gets a reference to the key that would be used when inserting a value
1604
- /// through the VacantEntry.
1759
+ /// through the `VacantEntry`.
1760
+ ///
1761
+ /// # Examples
1762
+ ///
1763
+ /// ```
1764
+ /// use std::collections::HashMap;
1765
+ ///
1766
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1767
+ /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
1768
+ /// ```
1605
1769
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1606
1770
pub fn key ( & self ) -> & K {
1607
1771
& self . key
1608
1772
}
1609
1773
1610
1774
/// Take ownership of the key.
1775
+ ///
1776
+ /// # Examples
1777
+ ///
1778
+ /// ```
1779
+ /// #![feature(map_entry_recover_keys)]
1780
+ ///
1781
+ /// use std::collections::HashMap;
1782
+ /// use std::collections::hash_map::Entry;
1783
+ ///
1784
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1785
+ ///
1786
+ /// if let Entry::Vacant(v) = map.entry("poneyland") {
1787
+ /// v.into_key();
1788
+ /// }
1789
+ /// ```
1611
1790
#[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
1612
1791
pub fn into_key ( self ) -> K {
1613
1792
self . key
1614
1793
}
1615
1794
1616
1795
/// Sets the value of the entry with the VacantEntry's key,
1617
- /// and returns a mutable reference to it
1796
+ /// and returns a mutable reference to it.
1797
+ ///
1798
+ /// # Examples
1799
+ ///
1800
+ /// ```
1801
+ /// use std::collections::HashMap;
1802
+ /// use std::collections::hash_map::Entry;
1803
+ ///
1804
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1805
+ ///
1806
+ /// if let Entry::Vacant(o) = map.entry("poneyland") {
1807
+ /// o.insert(37);
1808
+ /// }
1809
+ /// assert_eq!(map["poneyland"], 37);
1810
+ /// ```
1618
1811
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1619
1812
pub fn insert ( self , value : V ) -> & ' a mut V {
1620
1813
match self . elem {
0 commit comments