Skip to content

hash_map::OccupiedEntry::remove_entry #20601

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,10 +1474,16 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
value
}

/// Takes the key and the value out of the entry, and returns it
#[unstable]
pub fn remove_entry(self) -> (K, V) {
pop_internal(self.elem)
}

#[stable]
/// Takes the value out of the entry, and returns it
pub fn remove(self) -> V {
pop_internal(self.elem).1
self.remove_entry().1
}
}

Expand Down Expand Up @@ -2151,6 +2157,15 @@ mod test_map {
assert_eq!(map.get(&3), None);
assert_eq!(map.len(), 5);

// Existing key (take entry)
match map.entry(&4) {
Vacant(_) => unreachable!(),
Occupied(view) => {
assert_eq!(view.remove_entry(), (4, 40));
}
}
assert_eq!(map.get(&4), None);
assert_eq!(map.len(), 4);

// Inexistent key (insert)
match map.entry(&10) {
Expand All @@ -2160,7 +2175,7 @@ mod test_map {
}
}
assert_eq!(map.get(&10).unwrap(), &1000);
assert_eq!(map.len(), 6);
assert_eq!(map.len(), 5);
}

#[test]
Expand Down