Repository navigation
Tracking issue for HashMap::extract_if and HashSet::extract_if #59618
Copy link
Copy link
Closed
Labels
A-collectionsArea: `std::collections`Area: `std::collections`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Milestone
Description
The feature gate for the issue is #![feature(hash_extract_if)] (previously hash_drain_filter)
Currently only Vec and LinkedList have a drain_filter method, while other collections such as HashMap and HashSet do not.
This means that currently, removing items from a collection, and getting ownership of those items is fairly...unidiomatic and cumbersome.
For references, see rust-lang/rfcs#2140
pub mod collections {
pub mod hash_map {
impl<K, V, S> HashMap<K, V, S> {
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool;
}
pub struct ExtractIf<'a, K, V, F>
where
F: FnMut(&K, &mut V) -> bool;
impl<K, V, F> Iterator for ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool;
impl<K, V, F> FusedIterator for ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool;
impl<K, V, F> Debug for ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool;
}
pub mod hash_set {
impl<T, S> HashSet<T, S> {
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, T, F>
where
F: FnMut(&T) -> bool;
}
pub struct ExtractIf<'a, K, F>
where
F: FnMut(&K) -> bool;
impl<K, F> Iterator for ExtractIf<'_, K, F>
where
F: FnMut(&K) -> bool;
impl<K, F> FusedIterator for ExtractIf<'_, K, F>
where
F: FnMut(&K) -> bool;
impl<K, F> Debug for ExtractIf<'_, K, F>
where
F: FnMut(&K) -> bool;
}
}Implementation History
- Add drain_filter method to HashMap and HashSet #76458
- removed drain-on-drop behavior, renamed to extract_if
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-collectionsArea: `std::collections`Area: `std::collections`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Activity
hellow554 commentedon Apr 2, 2019
Related #42849
mbrubeck commentedon Jul 9, 2020
This may be easy to implement since
drain_filterwas added to the underlyinghashbrowntypes in rust-lang/hashbrown#135 and rust-lang/hashbrown#179. (Requireshashbrown0.8.1 or later; see #70052.)[-]Add drain_filter to HashMap, HashSet, and other collections[/-][+]Add drain_filter to HashMap and HashSet[/+]mbrubeck commentedon Aug 11, 2020
I have implemented this on a branch, but it will need to wait for a new release of the
hashbrowncrate because it depends on rust-lang/hashbrown#185, rust-lang/hashbrown#187, and rust-lang/hashbrown#188:master...mbrubeck:hash_drain_filter
24 remaining items
rfcbot commentedon Feb 11, 2025
🔔 This is now entering its final comment period, as per the review above. 🔔
rfcbot commentedon Feb 21, 2025
The final comment period, with a disposition to merge, as per the review above, is now complete.
As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.
This will be merged soon.
Rollup merge of rust-lang#134655 - GrigorenkoPV:hash_extract_if, r=cu…
Unrolled build for rust-lang#134655
Rollup merge of #134655 - GrigorenkoPV:hash_extract_if, r=cuviper
Rollup merge of rust-lang#134655 - GrigorenkoPV:hash_extract_if, r=cu…
Rollup merge of rust-lang#134655 - GrigorenkoPV:hash_extract_if, r=cu…