@@ -313,6 +313,10 @@ pub struct RangeMut<'a, K: 'a, V: 'a> {
313
313
}
314
314
315
315
/// A view into a single entry in a map, which may either be vacant or occupied.
316
+ /// This enum is constructed from the [`entry`] method on [`BTreeMap`].
317
+ ///
318
+ /// [`BTreeMap`]: struct.BTreeMap.html
319
+ /// [`entry`]: struct.BTreeMap.html#method.entry
316
320
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
317
321
pub enum Entry < ' a , K : ' a , V : ' a > {
318
322
/// A vacant Entry
@@ -340,7 +344,9 @@ impl<'a, K: 'a + Debug + Ord, V: 'a + Debug> Debug for Entry<'a, K, V> {
340
344
}
341
345
}
342
346
343
- /// A vacant Entry.
347
+ /// A vacant Entry. It is part of the [`Entry`] enum.
348
+ ///
349
+ /// [`Entry`]: enum.Entry.html
344
350
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
345
351
pub struct VacantEntry < ' a , K : ' a , V : ' a > {
346
352
key : K ,
@@ -360,7 +366,9 @@ impl<'a, K: 'a + Debug + Ord, V: 'a> Debug for VacantEntry<'a, K, V> {
360
366
}
361
367
}
362
368
363
- /// An occupied Entry.
369
+ /// An occupied Entry. It is part of the [`Entry`] enum.
370
+ ///
371
+ /// [`Entry`]: enum.Entry.html
364
372
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
365
373
pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
366
374
handle : Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: KV > ,
@@ -1890,6 +1898,17 @@ impl<K, V> BTreeMap<K, V> {
1890
1898
impl < ' a , K : Ord , V > Entry < ' a , K , V > {
1891
1899
/// Ensures a value is in the entry by inserting the default if empty, and returns
1892
1900
/// a mutable reference to the value in the entry.
1901
+ ///
1902
+ /// # Examples
1903
+ ///
1904
+ /// ```
1905
+ /// use std::collections::BTreeMap;
1906
+ ///
1907
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
1908
+ /// map.entry("poneyland").or_insert(12);
1909
+ ///
1910
+ /// assert_eq!(map["poneyland"], 12);
1911
+ /// ```
1893
1912
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1894
1913
pub fn or_insert ( self , default : V ) -> & ' a mut V {
1895
1914
match self {
@@ -1900,6 +1919,19 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
1900
1919
1901
1920
/// Ensures a value is in the entry by inserting the result of the default function if empty,
1902
1921
/// and returns a mutable reference to the value in the entry.
1922
+ ///
1923
+ /// # Examples
1924
+ ///
1925
+ /// ```
1926
+ /// use std::collections::BTreeMap;
1927
+ ///
1928
+ /// let mut map: BTreeMap<&str, String> = BTreeMap::new();
1929
+ /// let s = "hoho".to_owned();
1930
+ ///
1931
+ /// map.entry("poneyland").or_insert_with(|| s);
1932
+ ///
1933
+ /// assert_eq!(map["poneyland"], "hoho".to_owned());
1934
+ /// ```
1903
1935
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1904
1936
pub fn or_insert_with < F : FnOnce ( ) -> V > ( self , default : F ) -> & ' a mut V {
1905
1937
match self {
@@ -1909,6 +1941,15 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
1909
1941
}
1910
1942
1911
1943
/// Returns a reference to this entry's key.
1944
+ ///
1945
+ /// # Examples
1946
+ ///
1947
+ /// ```
1948
+ /// use std::collections::BTreeMap;
1949
+ ///
1950
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
1951
+ /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
1952
+ /// ```
1912
1953
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1913
1954
pub fn key ( & self ) -> & K {
1914
1955
match * self {
@@ -1921,19 +1962,58 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
1921
1962
impl < ' a , K : Ord , V > VacantEntry < ' a , K , V > {
1922
1963
/// Gets a reference to the key that would be used when inserting a value
1923
1964
/// through the VacantEntry.
1965
+ ///
1966
+ /// # Examples
1967
+ ///
1968
+ /// ```
1969
+ /// use std::collections::BTreeMap;
1970
+ ///
1971
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
1972
+ /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
1973
+ /// ```
1924
1974
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1925
1975
pub fn key ( & self ) -> & K {
1926
1976
& self . key
1927
1977
}
1928
1978
1929
1979
/// Take ownership of the key.
1980
+ ///
1981
+ /// # Examples
1982
+ ///
1983
+ /// ```
1984
+ /// #![feature(map_entry_recover_keys)]
1985
+ ///
1986
+ /// use std::collections::BTreeMap;
1987
+ /// use std::collections::btree_map::Entry;
1988
+ ///
1989
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
1990
+ ///
1991
+ /// if let Entry::Vacant(v) = map.entry("poneyland") {
1992
+ /// v.into_key();
1993
+ /// }
1994
+ /// ```
1930
1995
#[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
1931
1996
pub fn into_key ( self ) -> K {
1932
1997
self . key
1933
1998
}
1934
1999
1935
2000
/// Sets the value of the entry with the VacantEntry's key,
1936
2001
/// and returns a mutable reference to it.
2002
+ ///
2003
+ /// # Examples
2004
+ ///
2005
+ /// ```
2006
+ /// use std::collections::BTreeMap;
2007
+ ///
2008
+ /// let mut count: BTreeMap<&str, usize> = BTreeMap::new();
2009
+ ///
2010
+ /// // count the number of occurrences of letters in the vec
2011
+ /// for x in vec!["a","b","a","c","a","b"] {
2012
+ /// *count.entry(x).or_insert(0) += 1;
2013
+ /// }
2014
+ ///
2015
+ /// assert_eq!(count["a"], 3);
2016
+ /// ```
1937
2017
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1938
2018
pub fn insert ( self , value : V ) -> & ' a mut V {
1939
2019
* self . length += 1 ;
@@ -1979,43 +2059,150 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
1979
2059
1980
2060
impl < ' a , K : Ord , V > OccupiedEntry < ' a , K , V > {
1981
2061
/// Gets a reference to the key in the entry.
2062
+ ///
2063
+ /// # Examples
2064
+ ///
2065
+ /// ```
2066
+ /// use std::collections::BTreeMap;
2067
+ ///
2068
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
2069
+ /// map.entry("poneyland").or_insert(12);
2070
+ /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
2071
+ /// ```
1982
2072
#[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
1983
2073
pub fn key ( & self ) -> & K {
1984
2074
self . handle . reborrow ( ) . into_kv ( ) . 0
1985
2075
}
1986
2076
1987
2077
/// Take ownership of the key and value from the map.
2078
+ ///
2079
+ /// # Examples
2080
+ ///
2081
+ /// ```
2082
+ /// #![feature(map_entry_recover_keys)]
2083
+ ///
2084
+ /// use std::collections::BTreeMap;
2085
+ /// use std::collections::btree_map::Entry;
2086
+ ///
2087
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
2088
+ /// map.entry("poneyland").or_insert(12);
2089
+ ///
2090
+ /// if let Entry::Occupied(o) = map.entry("poneyland") {
2091
+ /// // We delete the entry from the map.
2092
+ /// o.remove_pair();
2093
+ /// }
2094
+ ///
2095
+ /// // If now try to get the value, it will panic:
2096
+ /// // println!("{}", map["poneyland"]);
2097
+ /// ```
1988
2098
#[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
1989
2099
pub fn remove_pair ( self ) -> ( K , V ) {
1990
2100
self . remove_kv ( )
1991
2101
}
1992
2102
1993
2103
/// Gets a reference to the value in the entry.
2104
+ ///
2105
+ /// # Examples
2106
+ ///
2107
+ /// ```
2108
+ /// use std::collections::BTreeMap;
2109
+ /// use std::collections::btree_map::Entry;
2110
+ ///
2111
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
2112
+ /// map.entry("poneyland").or_insert(12);
2113
+ ///
2114
+ /// if let Entry::Occupied(o) = map.entry("poneyland") {
2115
+ /// assert_eq!(o.get(), &12);
2116
+ /// }
2117
+ /// ```
1994
2118
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1995
2119
pub fn get ( & self ) -> & V {
1996
2120
self . handle . reborrow ( ) . into_kv ( ) . 1
1997
2121
}
1998
2122
1999
2123
/// Gets a mutable reference to the value in the entry.
2124
+ ///
2125
+ /// # Examples
2126
+ ///
2127
+ /// ```
2128
+ /// use std::collections::BTreeMap;
2129
+ /// use std::collections::btree_map::Entry;
2130
+ ///
2131
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
2132
+ /// map.entry("poneyland").or_insert(12);
2133
+ ///
2134
+ /// assert_eq!(map["poneyland"], 12);
2135
+ /// if let Entry::Occupied(mut o) = map.entry("poneyland") {
2136
+ /// *o.get_mut() += 10;
2137
+ /// }
2138
+ /// assert_eq!(map["poneyland"], 22);
2139
+ /// ```
2000
2140
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2001
2141
pub fn get_mut ( & mut self ) -> & mut V {
2002
2142
self . handle . kv_mut ( ) . 1
2003
2143
}
2004
2144
2005
2145
/// Converts the entry into a mutable reference to its value.
2146
+ ///
2147
+ /// # Examples
2148
+ ///
2149
+ /// ```
2150
+ /// use std::collections::BTreeMap;
2151
+ /// use std::collections::btree_map::Entry;
2152
+ ///
2153
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
2154
+ /// map.entry("poneyland").or_insert(12);
2155
+ ///
2156
+ /// assert_eq!(map["poneyland"], 12);
2157
+ /// if let Entry::Occupied(o) = map.entry("poneyland") {
2158
+ /// *o.into_mut() += 10;
2159
+ /// }
2160
+ /// assert_eq!(map["poneyland"], 22);
2161
+ /// ```
2006
2162
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2007
2163
pub fn into_mut ( self ) -> & ' a mut V {
2008
2164
self . handle . into_kv_mut ( ) . 1
2009
2165
}
2010
2166
2011
2167
/// Sets the value of the entry with the OccupiedEntry's key,
2012
2168
/// and returns the entry's old value.
2169
+ ///
2170
+ /// # Examples
2171
+ ///
2172
+ /// ```
2173
+ /// use std::collections::BTreeMap;
2174
+ /// use std::collections::btree_map::Entry;
2175
+ ///
2176
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
2177
+ /// map.entry("poneyland").or_insert(12);
2178
+ ///
2179
+ /// if let Entry::Occupied(mut o) = map.entry("poneyland") {
2180
+ /// assert_eq!(o.insert(15), 12);
2181
+ /// }
2182
+ /// assert_eq!(map["poneyland"], 15);
2183
+ /// ```
2013
2184
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2014
2185
pub fn insert ( & mut self , value : V ) -> V {
2015
2186
mem:: replace ( self . get_mut ( ) , value)
2016
2187
}
2017
2188
2018
2189
/// Takes the value of the entry out of the map, and returns it.
2190
+ ///
2191
+ /// # Examples
2192
+ ///
2193
+ /// ```
2194
+ /// use std::collections::BTreeMap;
2195
+ /// use std::collections::btree_map::Entry;
2196
+ ///
2197
+ /// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
2198
+ /// map.entry("poneyland").or_insert(12);
2199
+ ///
2200
+ /// if let Entry::Occupied(o) = map.entry("poneyland") {
2201
+ /// assert_eq!(o.remove(), 12);
2202
+ /// }
2203
+ /// // If we try to get "poneyland"'s value, it'll panic:
2204
+ /// // println!("{}", map["poneyland"]);
2205
+ /// ```
2019
2206
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2020
2207
pub fn remove ( self ) -> V {
2021
2208
self . remove_kv ( ) . 1
0 commit comments