Skip to content
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

drain_filter for all collections #2140

Open
6 tasks
Havvy opened this issue Sep 4, 2017 · 6 comments
Open
6 tasks

drain_filter for all collections #2140

Havvy opened this issue Sep 4, 2017 · 6 comments
Labels
A-collections Proposals about collection APIs T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@Havvy
Copy link
Contributor

Havvy commented Sep 4, 2017

For all collections, provide a method drain_filter. For some discussion of the method name and signature, see rust-lang/rust#43244, especially this comment.

Status

Implemented, awaiting stabilization

Unimplemented

Description

This method mutably borrows the collection and takes a predicate on the elements of the collection (e.g. values for lists and key-value pairs for maps). It then creates an iterator over the elements of the collection where the predicate returned truthfully and accessing that element in the iterator removes it from the collection. For ordered collections, it should return them in order.

Example usage

let v = vec![1, 2, 3, 4, 5, 6, 7, 8];
let primes = v.drain_filter(|n| n.is_prime()).collect::Vec<_>();
assert!(v == vec![1, 4, 6, 8]);
assert!(primes == vec![2, 3, 5, 7]);

Related ideas

Also of use would be a method that finds and removes the first element matching a predicate, as it would not have to do things like pre-poop its pants in case the Drain Where iterator leaks nor would it have to traverse the entire collection if e.g. the first element checked matches the predicate.

@eddyb
Copy link
Member

eddyb commented Sep 4, 2017

For Vec see rust-lang/rust#43245 - although I believe all collections can eventually have this.

@Havvy Havvy changed the title Predicately remove elements from collections drain_filter for all collections Sep 4, 2017
@mark-i-m
Copy link
Member

mark-i-m commented Sep 5, 2017

I can't say how many times I've needed something like this!

@Centril Centril added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Dec 6, 2017
@jonas-schievink jonas-schievink added the A-collections Proposals about collection APIs label Apr 1, 2019
tmandry added a commit to tmandry/rust that referenced this issue Sep 10, 2020
Add drain_filter method to HashMap and HashSet

Add `HashMap::drain_filter` and `HashSet::drain_filter`, implementing part of rust-lang/rfcs#2140.  These new methods are unstable.  The tracking issue is rust-lang#59618.

The added iterators behave the same as `BTreeMap::drain_filter` and `BTreeSet::drain_filter`, except their iteration order is arbitrary.  The unit tests are adapted from `alloc::collections::btree`.

This branch rewrites `HashSet` to be a wrapper around `hashbrown::HashSet` rather than `std::collections::HashMap`.
 (Both are themselves wrappers around `hashbrown::HashMap`, so the in-memory representation is the same either way.)  This lets `std` re-use more iterator code from `hashbrown`.  Without this change, we would need to duplicate much more code to implement `HashSet::drain_filter`.

This branch also updates the `hashbrown` crate to version 0.9.0.  Aside from changes related to the `DrainFilter` iterators, this version only changes features that are not used in libstd or rustc.  And it updates `indexmap` to version 1.6.0, whose only change is compatibility with `hashbrown` 0.9.0.
@WaffleLapkin
Copy link
Member

VecDeque doesn't have drain_filter either. Shouldn't it be included in the unimplemented list?

@Boscop
Copy link

Boscop commented Oct 9, 2020

@WaffleLapkin VecDeque doesn't have splice either (or something similar that allows extending at the front), but it should!
(SliceDeque supports splice and drain_filter but doesn't work on wasm.)

@mbrubeck
Copy link
Contributor

Added VecDeque to the checklist above.

@Boscop
Copy link

Boscop commented Nov 14, 2020

@mbrubeck What about String::drain_filter? It has retain whose closure gets a char (not &mut char), so we need drain_filter as well for String.

eclairevoyant added a commit to eclairevoyant/aur-packages that referenced this issue Jun 23, 2023
archlinux-github pushed a commit to archlinux/aur that referenced this issue Jun 23, 2023
eclairevoyant added a commit to eclairevoyant/eww that referenced this issue Jun 23, 2023
eclairevoyant added a commit to eclairevoyant/eww that referenced this issue Jul 15, 2023
eclairevoyant added a commit to eclairevoyant/eww that referenced this issue Jul 15, 2023
eclairevoyant added a commit to eclairevoyant/eww that referenced this issue Jul 15, 2023
elkowar pushed a commit to elkowar/eww that referenced this issue Jul 29, 2023
diegoatpitch pushed a commit to diegoatpitch/eww that referenced this issue Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Proposals about collection APIs T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

8 participants