Skip to content

Tracking issue for HashMap::extract_if and HashSet::extract_if #59618

Closed

Description

@DutchGhost

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

Activity

  1. hellow554 commented on Apr 2, 2019

    @hellow554
    Contributor

    Related #42849

  2. mbrubeck commented on Jul 9, 2020

    @mbrubeck
    Contributor

    This may be easy to implement since drain_filter was added to the underlying hashbrown types in rust-lang/hashbrown#135 and rust-lang/hashbrown#179. (Requires hashbrown 0.8.1 or later; see #70052.)

  3. changed the title [-]Add drain_filter to HashMap, HashSet, and other collections[/-] [+]Add drain_filter to HashMap and HashSet[/+] on Aug 11, 2020
  4. mbrubeck commented on Aug 11, 2020

    @mbrubeck
    Contributor

    I have implemented this on a branch, but it will need to wait for a new release of the hashbrown crate because it depends on rust-lang/hashbrown#185, rust-lang/hashbrown#187, and rust-lang/hashbrown#188:

    master...mbrubeck:hash_drain_filter

  5. 24 remaining items

  6. added
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    and removed
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    on Feb 11, 2025
  7. rfcbot commented on Feb 11, 2025

    @rfcbot

    🔔 This is now entering its final comment period, as per the review above. 🔔

  8. added
    to-announceAnnounce this issue on triage meeting
    and removed
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    on Feb 21, 2025
  9. rfcbot commented on Feb 21, 2025

    @rfcbot

    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.

  10. added a commit that references this issue on Feb 24, 2025
  11. added a commit that references this issue on Feb 25, 2025
  12. added 2 commits that reference this issue on Mar 11, 2025
  13. added this to the 1.87.0 milestone on Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis 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.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions