@@ -1741,6 +1741,20 @@ where
1741
1741
A : Default + Allocator + Clone ,
1742
1742
{
1743
1743
/// Creates an empty `HashMap<K, V, S, A>`, with the `Default` value for the hasher and allocator.
1744
+ ///
1745
+ /// # Examples
1746
+ ///
1747
+ /// ```
1748
+ /// use hashbrown::HashMap;
1749
+ /// use std::collections::hash_map::RandomState;
1750
+ ///
1751
+ /// // You can specify all types of HashMap, including hasher and allocator.
1752
+ /// // Created map is empty and don't allocate memory
1753
+ /// let map: HashMap<u32, String> = Default::default();
1754
+ /// assert_eq!(map.capacity(), 0);
1755
+ /// let map: HashMap<u32, String, RandomState> = HashMap::default();
1756
+ /// assert_eq!(map.capacity(), 0);
1757
+ /// ```
1744
1758
#[ cfg_attr( feature = "inline-more" , inline) ]
1745
1759
fn default ( ) -> Self {
1746
1760
Self :: with_hasher_in ( Default :: default ( ) , Default :: default ( ) )
@@ -1761,6 +1775,17 @@ where
1761
1775
/// # Panics
1762
1776
///
1763
1777
/// Panics if the key is not present in the `HashMap`.
1778
+ ///
1779
+ /// # Examples
1780
+ ///
1781
+ /// ```
1782
+ /// use hashbrown::HashMap;
1783
+ ///
1784
+ /// let map: HashMap<_, _> = [("a", "One"), ("b", "Two")].into();
1785
+ ///
1786
+ /// assert_eq!(map[&"a"], "One");
1787
+ /// assert_eq!(map[&"b"], "Two");
1788
+ /// ```
1764
1789
#[ cfg_attr( feature = "inline-more" , inline) ]
1765
1790
fn index ( & self , key : & Q ) -> & V {
1766
1791
self . get ( key) . expect ( "no entry found for key" )
@@ -1788,13 +1813,34 @@ where
1788
1813
}
1789
1814
}
1790
1815
1791
- /// An iterator over the entries of a `HashMap`.
1792
- ///
1816
+ /// An iterator over the entries of a `HashMap` in arbitrary order.
1817
+ /// The iterator element type is `(&'a K, &'a V)`.
1818
+ ///
1793
1819
/// This `struct` is created by the [`iter`] method on [`HashMap`]. See its
1794
1820
/// documentation for more.
1795
1821
///
1796
1822
/// [`iter`]: struct.HashMap.html#method.iter
1797
1823
/// [`HashMap`]: struct.HashMap.html
1824
+ ///
1825
+ /// # Example
1826
+ ///
1827
+ /// ```
1828
+ /// use hashbrown::HashMap;
1829
+ ///
1830
+ /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into();
1831
+ ///
1832
+ /// let mut iter = map.iter();
1833
+ /// let mut vec = vec![iter.next(), iter.next(), iter.next()];
1834
+ ///
1835
+ /// // The `Iter` iterator produces items in arbitrary order, so the
1836
+ /// // items must be sorted to test them against a sorted array.
1837
+ /// vec.sort_unstable();
1838
+ /// assert_eq!(vec, [Some((&1, &"a")), Some((&2, &"b")), Some((&3, &"c"))]);
1839
+ ///
1840
+ /// // It is fused iterator
1841
+ /// assert_eq!(iter.next(), None);
1842
+ /// assert_eq!(iter.next(), None);
1843
+ /// ```
1798
1844
pub struct Iter < ' a , K , V > {
1799
1845
inner : RawIter < ( K , V ) > ,
1800
1846
marker : PhantomData < ( & ' a K , & ' a V ) > ,
@@ -1817,13 +1863,33 @@ impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
1817
1863
}
1818
1864
}
1819
1865
1820
- /// A mutable iterator over the entries of a `HashMap`.
1866
+ /// A mutable iterator over the entries of a `HashMap` in arbitrary order.
1867
+ /// The iterator element type is `(&'a K, &'a mut V)`.
1821
1868
///
1822
1869
/// This `struct` is created by the [`iter_mut`] method on [`HashMap`]. See its
1823
1870
/// documentation for more.
1824
1871
///
1825
1872
/// [`iter_mut`]: struct.HashMap.html#method.iter_mut
1826
1873
/// [`HashMap`]: struct.HashMap.html
1874
+ ///
1875
+ /// # Example
1876
+ ///
1877
+ /// ```
1878
+ /// use hashbrown::HashMap;
1879
+ ///
1880
+ /// let mut map: HashMap<_, _> = [(1, "One".to_owned()), (2, "Two".into())].into();
1881
+ ///
1882
+ /// let mut iter = map.iter_mut();
1883
+ /// iter.next().map(|(_, v)| v.push_str(" Mississippi"));
1884
+ /// iter.next().map(|(_, v)| v.push_str(" Mississippi"));
1885
+ ///
1886
+ /// // It is fused iterator
1887
+ /// assert_eq!(iter.next(), None);
1888
+ /// assert_eq!(iter.next(), None);
1889
+ ///
1890
+ /// assert_eq!(map.get(&1).unwrap(), &"One Mississippi".to_owned());
1891
+ /// assert_eq!(map.get(&2).unwrap(), &"Two Mississippi".to_owned());
1892
+ /// ```
1827
1893
pub struct IterMut < ' a , K , V > {
1828
1894
inner : RawIter < ( K , V ) > ,
1829
1895
// To ensure invariance with respect to V
@@ -1846,13 +1912,35 @@ impl<K, V> IterMut<'_, K, V> {
1846
1912
}
1847
1913
}
1848
1914
1849
- /// An owning iterator over the entries of a `HashMap`.
1915
+ /// An owning iterator over the entries of a `HashMap` in arbitrary order.
1916
+ /// The iterator element type is `(K, V)`.
1850
1917
///
1851
1918
/// This `struct` is created by the [`into_iter`] method on [`HashMap`]
1852
1919
/// (provided by the `IntoIterator` trait). See its documentation for more.
1920
+ /// The map cannot be used after calling that method.
1853
1921
///
1854
1922
/// [`into_iter`]: struct.HashMap.html#method.into_iter
1855
1923
/// [`HashMap`]: struct.HashMap.html
1924
+ ///
1925
+ /// # Example
1926
+ ///
1927
+ /// ```
1928
+ /// use hashbrown::HashMap;
1929
+ ///
1930
+ /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into();
1931
+ ///
1932
+ /// let mut iter = map.into_iter();
1933
+ /// let mut vec = vec![iter.next(), iter.next(), iter.next()];
1934
+ ///
1935
+ /// // The `IntoIter` iterator produces items in arbitrary order, so the
1936
+ /// // items must be sorted to test them against a sorted array.
1937
+ /// vec.sort_unstable();
1938
+ /// assert_eq!(vec, [Some((1, "a")), Some((2, "b")), Some((3, "c"))]);
1939
+ ///
1940
+ /// // It is fused iterator
1941
+ /// assert_eq!(iter.next(), None);
1942
+ /// assert_eq!(iter.next(), None);
1943
+ /// ```
1856
1944
pub struct IntoIter < K , V , A : Allocator + Clone = Global > {
1857
1945
inner : RawIntoIter < ( K , V ) , A > ,
1858
1946
}
@@ -1868,13 +1956,35 @@ impl<K, V, A: Allocator + Clone> IntoIter<K, V, A> {
1868
1956
}
1869
1957
}
1870
1958
1871
- /// An owning iterator over the keys of a `HashMap`.
1959
+ /// An owning iterator over the keys of a `HashMap` in arbitrary order.
1960
+ /// The iterator element type is `K`.
1872
1961
///
1873
1962
/// This `struct` is created by the [`into_keys`] method on [`HashMap`].
1874
1963
/// See its documentation for more.
1875
- ///
1964
+ /// The map cannot be used after calling that method.
1965
+ ///
1876
1966
/// [`into_keys`]: struct.HashMap.html#method.into_keys
1877
1967
/// [`HashMap`]: struct.HashMap.html
1968
+ ///
1969
+ /// # Example
1970
+ ///
1971
+ /// ```
1972
+ /// use hashbrown::HashMap;
1973
+ ///
1974
+ /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into();
1975
+ ///
1976
+ /// let mut keys = map.into_keys();
1977
+ /// let mut vec = vec![keys.next(), keys.next(), keys.next()];
1978
+ ///
1979
+ /// // The `IntoKeys` iterator produces keys in arbitrary order, so the
1980
+ /// // keys must be sorted to test them against a sorted array.
1981
+ /// vec.sort_unstable();
1982
+ /// assert_eq!(vec, [Some(1), Some(2), Some(3)]);
1983
+ ///
1984
+ /// // It is fused iterator
1985
+ /// assert_eq!(keys.next(), None);
1986
+ /// assert_eq!(keys.next(), None);
1987
+ /// ```
1878
1988
pub struct IntoKeys < K , V , A : Allocator + Clone = Global > {
1879
1989
inner : IntoIter < K , V , A > ,
1880
1990
}
@@ -1909,13 +2019,34 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> fmt::Debug for IntoKeys<K, V, A>
1909
2019
}
1910
2020
}
1911
2021
1912
- /// An owning iterator over the values of a `HashMap`.
2022
+ /// An owning iterator over the values of a `HashMap` in arbitrary order.
2023
+ /// The iterator element type is `V`.
1913
2024
///
1914
2025
/// This `struct` is created by the [`into_values`] method on [`HashMap`].
1915
- /// See its documentation for more.
2026
+ /// See its documentation for more. The map cannot be used after calling that method.
1916
2027
///
1917
2028
/// [`into_values`]: struct.HashMap.html#method.into_values
1918
2029
/// [`HashMap`]: struct.HashMap.html
2030
+ ///
2031
+ /// # Example
2032
+ ///
2033
+ /// ```
2034
+ /// use hashbrown::HashMap;
2035
+ ///
2036
+ /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into();
2037
+ ///
2038
+ /// let mut values = map.into_values();
2039
+ /// let mut vec = vec![values.next(), values.next(), values.next()];
2040
+ ///
2041
+ /// // The `IntoValues` iterator produces values in arbitrary order, so
2042
+ /// // the values must be sorted to test them against a sorted array.
2043
+ /// vec.sort_unstable();
2044
+ /// assert_eq!(vec, [Some("a"), Some("b"), Some("c")]);
2045
+ ///
2046
+ /// // It is fused iterator
2047
+ /// assert_eq!(values.next(), None);
2048
+ /// assert_eq!(values.next(), None);
2049
+ /// ```
1919
2050
pub struct IntoValues < K , V , A : Allocator + Clone = Global > {
1920
2051
inner : IntoIter < K , V , A > ,
1921
2052
}
@@ -1950,13 +2081,34 @@ impl<K, V: Debug, A: Allocator + Clone> fmt::Debug for IntoValues<K, V, A> {
1950
2081
}
1951
2082
}
1952
2083
1953
- /// An iterator over the keys of a `HashMap`.
1954
- ///
2084
+ /// An iterator over the keys of a `HashMap` in arbitrary order.
2085
+ /// The iterator element type is `&'a K`.
2086
+ ///
1955
2087
/// This `struct` is created by the [`keys`] method on [`HashMap`]. See its
1956
2088
/// documentation for more.
1957
2089
///
1958
2090
/// [`keys`]: struct.HashMap.html#method.keys
1959
2091
/// [`HashMap`]: struct.HashMap.html
2092
+ ///
2093
+ /// # Example
2094
+ ///
2095
+ /// ```
2096
+ /// use hashbrown::HashMap;
2097
+ ///
2098
+ /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into();
2099
+ ///
2100
+ /// let mut keys = map.keys();
2101
+ /// let mut vec = vec![keys.next(), keys.next(), keys.next()];
2102
+ ///
2103
+ /// // The `Keys` iterator produces keys in arbitrary order, so the
2104
+ /// // keys must be sorted to test them against a sorted array.
2105
+ /// vec.sort_unstable();
2106
+ /// assert_eq!(vec, [Some(&1), Some(&2), Some(&3)]);
2107
+ ///
2108
+ /// // It is fused iterator
2109
+ /// assert_eq!(keys.next(), None);
2110
+ /// assert_eq!(keys.next(), None);
2111
+ /// ```
1960
2112
pub struct Keys < ' a , K , V > {
1961
2113
inner : Iter < ' a , K , V > ,
1962
2114
}
@@ -1977,13 +2129,34 @@ impl<K: Debug, V> fmt::Debug for Keys<'_, K, V> {
1977
2129
}
1978
2130
}
1979
2131
1980
- /// An iterator over the values of a `HashMap`.
2132
+ /// An iterator over the values of a `HashMap` in arbitrary order.
2133
+ /// The iterator element type is `&'a V`.
1981
2134
///
1982
2135
/// This `struct` is created by the [`values`] method on [`HashMap`]. See its
1983
2136
/// documentation for more.
1984
2137
///
1985
2138
/// [`values`]: struct.HashMap.html#method.values
1986
2139
/// [`HashMap`]: struct.HashMap.html
2140
+ ///
2141
+ /// # Example
2142
+ ///
2143
+ /// ```
2144
+ /// use hashbrown::HashMap;
2145
+ ///
2146
+ /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into();
2147
+ ///
2148
+ /// let mut values = map.values();
2149
+ /// let mut vec = vec![values.next(), values.next(), values.next()];
2150
+ ///
2151
+ /// // The `Values` iterator produces values in arbitrary order, so the
2152
+ /// // values must be sorted to test them against a sorted array.
2153
+ /// vec.sort_unstable();
2154
+ /// assert_eq!(vec, [Some(&"a"), Some(&"b"), Some(&"c")]);
2155
+ ///
2156
+ /// // It is fused iterator
2157
+ /// assert_eq!(values.next(), None);
2158
+ /// assert_eq!(values.next(), None);
2159
+ /// ```
1987
2160
pub struct Values < ' a , K , V > {
1988
2161
inner : Iter < ' a , K , V > ,
1989
2162
}
0 commit comments