Skip to content

Commit ff28448

Browse files
committed
Improving documentation. Part 2.
Continuation of rust-lang#328.
1 parent 426d5ea commit ff28448

File tree

1 file changed

+184
-11
lines changed

1 file changed

+184
-11
lines changed

src/map.rs

Lines changed: 184 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,20 @@ where
17411741
A: Default + Allocator + Clone,
17421742
{
17431743
/// 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+
/// ```
17441758
#[cfg_attr(feature = "inline-more", inline)]
17451759
fn default() -> Self {
17461760
Self::with_hasher_in(Default::default(), Default::default())
@@ -1761,6 +1775,17 @@ where
17611775
/// # Panics
17621776
///
17631777
/// 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+
/// ```
17641789
#[cfg_attr(feature = "inline-more", inline)]
17651790
fn index(&self, key: &Q) -> &V {
17661791
self.get(key).expect("no entry found for key")
@@ -1788,13 +1813,34 @@ where
17881813
}
17891814
}
17901815

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+
///
17931819
/// This `struct` is created by the [`iter`] method on [`HashMap`]. See its
17941820
/// documentation for more.
17951821
///
17961822
/// [`iter`]: struct.HashMap.html#method.iter
17971823
/// [`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+
/// ```
17981844
pub struct Iter<'a, K, V> {
17991845
inner: RawIter<(K, V)>,
18001846
marker: PhantomData<(&'a K, &'a V)>,
@@ -1817,13 +1863,33 @@ impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
18171863
}
18181864
}
18191865

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)`.
18211868
///
18221869
/// This `struct` is created by the [`iter_mut`] method on [`HashMap`]. See its
18231870
/// documentation for more.
18241871
///
18251872
/// [`iter_mut`]: struct.HashMap.html#method.iter_mut
18261873
/// [`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+
/// ```
18271893
pub struct IterMut<'a, K, V> {
18281894
inner: RawIter<(K, V)>,
18291895
// To ensure invariance with respect to V
@@ -1846,13 +1912,35 @@ impl<K, V> IterMut<'_, K, V> {
18461912
}
18471913
}
18481914

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)`.
18501917
///
18511918
/// This `struct` is created by the [`into_iter`] method on [`HashMap`]
18521919
/// (provided by the `IntoIterator` trait). See its documentation for more.
1920+
/// The map cannot be used after calling that method.
18531921
///
18541922
/// [`into_iter`]: struct.HashMap.html#method.into_iter
18551923
/// [`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+
/// ```
18561944
pub struct IntoIter<K, V, A: Allocator + Clone = Global> {
18571945
inner: RawIntoIter<(K, V), A>,
18581946
}
@@ -1868,13 +1956,35 @@ impl<K, V, A: Allocator + Clone> IntoIter<K, V, A> {
18681956
}
18691957
}
18701958

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`.
18721961
///
18731962
/// This `struct` is created by the [`into_keys`] method on [`HashMap`].
18741963
/// See its documentation for more.
1875-
///
1964+
/// The map cannot be used after calling that method.
1965+
///
18761966
/// [`into_keys`]: struct.HashMap.html#method.into_keys
18771967
/// [`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+
/// ```
18781988
pub struct IntoKeys<K, V, A: Allocator + Clone = Global> {
18791989
inner: IntoIter<K, V, A>,
18801990
}
@@ -1909,13 +2019,34 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> fmt::Debug for IntoKeys<K, V, A>
19092019
}
19102020
}
19112021

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`.
19132024
///
19142025
/// 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.
19162027
///
19172028
/// [`into_values`]: struct.HashMap.html#method.into_values
19182029
/// [`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+
/// ```
19192050
pub struct IntoValues<K, V, A: Allocator + Clone = Global> {
19202051
inner: IntoIter<K, V, A>,
19212052
}
@@ -1950,13 +2081,34 @@ impl<K, V: Debug, A: Allocator + Clone> fmt::Debug for IntoValues<K, V, A> {
19502081
}
19512082
}
19522083

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+
///
19552087
/// This `struct` is created by the [`keys`] method on [`HashMap`]. See its
19562088
/// documentation for more.
19572089
///
19582090
/// [`keys`]: struct.HashMap.html#method.keys
19592091
/// [`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+
/// ```
19602112
pub struct Keys<'a, K, V> {
19612113
inner: Iter<'a, K, V>,
19622114
}
@@ -1977,13 +2129,34 @@ impl<K: Debug, V> fmt::Debug for Keys<'_, K, V> {
19772129
}
19782130
}
19792131

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`.
19812134
///
19822135
/// This `struct` is created by the [`values`] method on [`HashMap`]. See its
19832136
/// documentation for more.
19842137
///
19852138
/// [`values`]: struct.HashMap.html#method.values
19862139
/// [`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+
/// ```
19872160
pub struct Values<'a, K, V> {
19882161
inner: Iter<'a, K, V>,
19892162
}

0 commit comments

Comments
 (0)