@@ -2030,6 +2030,31 @@ impl<'a, K, V> Entry<'a, K, V> {
2030
2030
Vacant ( entry) => Vacant ( entry) ,
2031
2031
}
2032
2032
}
2033
+
2034
+ /// Sets the value of the entry, and returns an OccupiedEntry.
2035
+ ///
2036
+ /// # Examples
2037
+ ///
2038
+ /// ```
2039
+ /// #![feature(entry_insert)]
2040
+ /// use std::collections::HashMap;
2041
+ ///
2042
+ /// let mut map: HashMap<&str, String> = HashMap::new();
2043
+ /// let entry = map.entry("poneyland").insert("hoho".to_string());
2044
+ ///
2045
+ /// assert_eq!(entry.key(), &"poneyland");
2046
+ /// ```
2047
+ #[ inline]
2048
+ #[ unstable( feature = "entry_insert" , issue = "65225" ) ]
2049
+ pub fn insert ( self , value : V ) -> OccupiedEntry < ' a , K , V > {
2050
+ match self {
2051
+ Occupied ( mut entry) => {
2052
+ entry. insert ( value) ;
2053
+ entry
2054
+ } ,
2055
+ Vacant ( entry) => entry. insert_entry ( value) ,
2056
+ }
2057
+ }
2033
2058
}
2034
2059
2035
2060
impl < ' a , K , V : Default > Entry < ' a , K , V > {
@@ -2347,6 +2372,28 @@ impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
2347
2372
pub fn insert ( self , value : V ) -> & ' a mut V {
2348
2373
self . base . insert ( value)
2349
2374
}
2375
+
2376
+ /// Sets the value of the entry with the VacantEntry's key,
2377
+ /// and returns an OccupiedEntry.
2378
+ ///
2379
+ /// # Examples
2380
+ ///
2381
+ /// ```
2382
+ /// use std::collections::HashMap;
2383
+ /// use std::collections::hash_map::Entry;
2384
+ ///
2385
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
2386
+ ///
2387
+ /// if let Entry::Vacant(o) = map.entry("poneyland") {
2388
+ /// o.insert(37);
2389
+ /// }
2390
+ /// assert_eq!(map["poneyland"], 37);
2391
+ /// ```
2392
+ #[ inline]
2393
+ fn insert_entry ( self , value : V ) -> OccupiedEntry < ' a , K , V > {
2394
+ let base = self . base . insert_entry ( value) ;
2395
+ OccupiedEntry { base }
2396
+ }
2350
2397
}
2351
2398
2352
2399
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments