@@ -2161,6 +2161,36 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
21612161 fn take_key ( & mut self ) -> Option < K > {
21622162 self . key . take ( )
21632163 }
2164+
2165+ /// Replaces the entry, returning the old key and value.
2166+ ///
2167+ /// # Examples
2168+ ///
2169+ /// ```
2170+ /// use std::collections::HashMap;
2171+ /// use std::collections::hash_map::Entry;
2172+ ///
2173+ /// let mut map: HashMap<String, u32> = HashMap::new();
2174+ /// map.insert(String::from("poneyland"), 15);
2175+ ///
2176+ /// if let Entry::Occupied(entry) = map.entry(String::from("poneyland")) {
2177+ /// let (old_key, old_value): (String, u32) = entry.replace(16);
2178+ /// assert_eq!(old_key, "poneyland");
2179+ /// assert_eq!(old_value, 15);
2180+ /// }
2181+ ///
2182+ /// assert_eq!(map.get("poneyland"), Some(&16));
2183+ ///
2184+ /// ```
2185+ #[ stable( feature = "rust1" , since = "1.20.0" ) ]
2186+ pub fn replace ( mut self , value : V ) -> ( K , V ) {
2187+ let ( old_key, old_value) = self . elem . read_mut ( ) ;
2188+
2189+ let old_key = mem:: replace ( old_key, self . key . unwrap ( ) ) ;
2190+ let old_value = mem:: replace ( old_value, value) ;
2191+
2192+ ( old_key, old_value)
2193+ }
21642194}
21652195
21662196impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
0 commit comments