-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.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
dima74, Dessix, timotree3, mxinden, nytopop and 63 more
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-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.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.