-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow replacing HashMap entries #44278
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2161,6 +2161,37 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { | |
fn take_key(&mut self) -> Option<K> { | ||
self.key.take() | ||
} | ||
|
||
/// Replaces the entry, returning the old key and value. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # #![feature(map_entry_replace)] | ||
/// use std::collections::HashMap; | ||
/// use std::collections::hash_map::Entry; | ||
/// | ||
/// let mut map: HashMap<String, u32> = HashMap::new(); | ||
/// map.insert(String::from("poneyland"), 15); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we use |
||
/// | ||
/// if let Entry::Occupied(entry) = map.entry(String::from("poneyland")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, use |
||
/// let (old_key, old_value): (String, u32) = entry.replace(16); | ||
/// assert_eq!(old_key, "poneyland"); | ||
/// assert_eq!(old_value, 15); | ||
/// } | ||
/// | ||
/// assert_eq!(map.get("poneyland"), Some(&16)); | ||
/// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove blank line? |
||
/// ``` | ||
#[unstable(feature = "map_entry_replace", issue = "44286")] | ||
pub fn replace(mut self, value: V) -> (K, V) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new API should be |
||
let (old_key, old_value) = self.elem.read_mut(); | ||
|
||
let old_key = mem::replace(old_key, self.key.unwrap()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One can take the key out of the entry using |
||
let old_value = mem::replace(old_value, value); | ||
|
||
(old_key, old_value) | ||
} | ||
} | ||
|
||
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we include the feature toggle in the docs so that end users know that it is necessary. So I'd remove the
#
prefix here.