@@ -313,6 +313,10 @@ pub struct RangeMut<'a, K: 'a, V: 'a> {
313313}
314314
315315/// 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
316320#[ stable( feature = "rust1" , since = "1.0.0" ) ]
317321pub enum Entry < ' a , K : ' a , V : ' a > {
318322 /// A vacant Entry
@@ -340,7 +344,9 @@ impl<'a, K: 'a + Debug + Ord, V: 'a + Debug> Debug for Entry<'a, K, V> {
340344 }
341345}
342346
343- /// A vacant Entry.
347+ /// A vacant Entry. It is part of the [`Entry`] enum.
348+ ///
349+ /// [`Entry`]: enum.Entry.html
344350#[ stable( feature = "rust1" , since = "1.0.0" ) ]
345351pub struct VacantEntry < ' a , K : ' a , V : ' a > {
346352 key : K ,
@@ -360,7 +366,9 @@ impl<'a, K: 'a + Debug + Ord, V: 'a> Debug for VacantEntry<'a, K, V> {
360366 }
361367}
362368
363- /// An occupied Entry.
369+ /// An occupied Entry. It is part of the [`Entry`] enum.
370+ ///
371+ /// [`Entry`]: enum.Entry.html
364372#[ stable( feature = "rust1" , since = "1.0.0" ) ]
365373pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
366374 handle : Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: KV > ,
@@ -1890,6 +1898,17 @@ impl<K, V> BTreeMap<K, V> {
18901898impl < ' a , K : Ord , V > Entry < ' a , K , V > {
18911899 /// Ensures a value is in the entry by inserting the default if empty, and returns
18921900 /// 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+ /// ```
18931912 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
18941913 pub fn or_insert ( self , default : V ) -> & ' a mut V {
18951914 match self {
@@ -1900,6 +1919,19 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
19001919
19011920 /// Ensures a value is in the entry by inserting the result of the default function if empty,
19021921 /// 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+ /// ```
19031935 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
19041936 pub fn or_insert_with < F : FnOnce ( ) -> V > ( self , default : F ) -> & ' a mut V {
19051937 match self {
@@ -1909,6 +1941,15 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
19091941 }
19101942
19111943 /// 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+ /// ```
19121953 #[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
19131954 pub fn key ( & self ) -> & K {
19141955 match * self {
@@ -1921,19 +1962,58 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
19211962impl < ' a , K : Ord , V > VacantEntry < ' a , K , V > {
19221963 /// Gets a reference to the key that would be used when inserting a value
19231964 /// 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+ /// ```
19241974 #[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
19251975 pub fn key ( & self ) -> & K {
19261976 & self . key
19271977 }
19281978
19291979 /// 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+ /// ```
19301995 #[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
19311996 pub fn into_key ( self ) -> K {
19321997 self . key
19331998 }
19341999
19352000 /// Sets the value of the entry with the VacantEntry's key,
19362001 /// 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+ /// ```
19372017 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
19382018 pub fn insert ( self , value : V ) -> & ' a mut V {
19392019 * self . length += 1 ;
@@ -1979,43 +2059,150 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
19792059
19802060impl < ' a , K : Ord , V > OccupiedEntry < ' a , K , V > {
19812061 /// 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+ /// ```
19822072 #[ stable( feature = "map_entry_keys" , since = "1.10.0" ) ]
19832073 pub fn key ( & self ) -> & K {
19842074 self . handle . reborrow ( ) . into_kv ( ) . 0
19852075 }
19862076
19872077 /// 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+ /// ```
19882098 #[ unstable( feature = "map_entry_recover_keys" , issue = "34285" ) ]
19892099 pub fn remove_pair ( self ) -> ( K , V ) {
19902100 self . remove_kv ( )
19912101 }
19922102
19932103 /// 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+ /// ```
19942118 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
19952119 pub fn get ( & self ) -> & V {
19962120 self . handle . reborrow ( ) . into_kv ( ) . 1
19972121 }
19982122
19992123 /// 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+ /// ```
20002140 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
20012141 pub fn get_mut ( & mut self ) -> & mut V {
20022142 self . handle . kv_mut ( ) . 1
20032143 }
20042144
20052145 /// 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+ /// ```
20062162 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
20072163 pub fn into_mut ( self ) -> & ' a mut V {
20082164 self . handle . into_kv_mut ( ) . 1
20092165 }
20102166
20112167 /// Sets the value of the entry with the OccupiedEntry's key,
20122168 /// 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+ /// ```
20132184 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
20142185 pub fn insert ( & mut self , value : V ) -> V {
20152186 mem:: replace ( self . get_mut ( ) , value)
20162187 }
20172188
20182189 /// 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+ /// ```
20192206 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
20202207 pub fn remove ( self ) -> V {
20212208 self . remove_kv ( ) . 1
0 commit comments