@@ -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" ) ]
13401344pub enum Entry < ' a , K : ' a , V : ' a > {
13411345 /// An occupied Entry.
@@ -1352,13 +1356,19 @@ pub enum Entry<'a, K: 'a, V: 'a> {
13521356}
13531357
13541358/// A view into a single occupied location in a HashMap.
1359+ /// It is part of the [`Entry`] enum.
1360+ ///
1361+ /// [`Entry`]: enum.Entry.html
13551362#[ stable( feature = "rust1" , since = "1.0.0" ) ]
13561363pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
13571364 key : Option < K > ,
13581365 elem : FullBucket < K , V , & ' a mut RawTable < K , V > > ,
13591366}
13601367
13611368/// A view into a single empty location in a HashMap.
1369+ /// It is part of the [`Entry`] enum.
1370+ ///
1371+ /// [`Entry`]: enum.Entry.html
13621372#[ stable( feature = "rust1" , since = "1.0.0" ) ]
13631373pub struct VacantEntry < ' a , K : ' a , V : ' a > {
13641374 hash : SafeHash ,
@@ -1518,6 +1528,20 @@ impl<'a, K, V> Entry<'a, K, V> {
15181528 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15191529 /// Ensures a value is in the entry by inserting the default if empty, and returns
15201530 /// 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+ /// ```
15211545 pub fn or_insert ( self , default : V ) -> & ' a mut V {
15221546 match self {
15231547 Occupied ( entry) => entry. into_mut ( ) ,
@@ -1528,6 +1552,19 @@ impl<'a, K, V> Entry<'a, K, V> {
15281552 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15291553 /// Ensures a value is in the entry by inserting the result of the default function if empty,
15301554 /// 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+ /// ```
15311568 pub fn or_insert_with < F : FnOnce ( ) -> V > ( self , default : F ) -> & ' a mut V {
15321569 match self {
15331570 Occupied ( entry) => entry. into_mut ( ) ,
@@ -1536,6 +1573,15 @@ impl<'a, K, V> Entry<'a, K, V> {
15361573 }
15371574
15381575 /// 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+ /// ```
15391585 #[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
15401586 pub fn key ( & self ) -> & K {
15411587 match * self {
@@ -1547,45 +1593,154 @@ impl<'a, K, V> Entry<'a, K, V> {
15471593
15481594impl < ' a , K , V > OccupiedEntry < ' a , K , V > {
15491595 /// 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+ /// ```
15501606 #[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
15511607 pub fn key ( & self ) -> & K {
15521608 self . elem . read ( ) . 0
15531609 }
15541610
15551611 /// 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+ /// ```
15561631 #[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
15571632 pub fn remove_pair ( self ) -> ( K , V ) {
15581633 pop_internal ( self . elem )
15591634 }
15601635
15611636 /// 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+ /// ```
15621651 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15631652 pub fn get ( & self ) -> & V {
15641653 self . elem . read ( ) . 1
15651654 }
15661655
15671656 /// 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+ /// ```
15681674 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15691675 pub fn get_mut ( & mut self ) -> & mut V {
15701676 self . elem . read_mut ( ) . 1
15711677 }
15721678
15731679 /// 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+ /// ```
15751698 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15761699 pub fn into_mut ( self ) -> & ' a mut V {
15771700 self . elem . into_mut_refs ( ) . 1
15781701 }
15791702
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+ /// ```
15811720 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15821721 pub fn insert ( & mut self , mut value : V ) -> V {
15831722 let old_value = self . get_mut ( ) ;
15841723 mem:: swap ( & mut value, old_value) ;
15851724 value
15861725 }
15871726
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+ /// ```
15891744 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15901745 pub fn remove ( self ) -> V {
15911746 pop_internal ( self . elem ) . 1
@@ -1601,20 +1756,58 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
16011756
16021757impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
16031758 /// 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+ /// ```
16051769 #[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
16061770 pub fn key ( & self ) -> & K {
16071771 & self . key
16081772 }
16091773
16101774 /// 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+ /// ```
16111790 #[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
16121791 pub fn into_key ( self ) -> K {
16131792 self . key
16141793 }
16151794
16161795 /// 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+ /// ```
16181811 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
16191812 pub fn insert ( self , value : V ) -> & ' a mut V {
16201813 match self . elem {
0 commit comments