rust-lang / rust-lang/rfcs

drain_filter_take

Open
#3,299 15 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Currently we have the unstable Vec::drain_filter:

pub fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, T, F, A>
where
    F: FnMut(&mut T) -> bool;

My use case is that I want to perform some side effects in filter, where the side effects would determine whether the item should be drained, but performing the side effects requires moving out the T if the element is to be filtered.

I propose a new method drain_filter_take that takes the form

pub fn drain_filter_take<F>(&mut self, filter: F) // does not return an iterator
where
    F: FnMut(T) -> Option<T>;

(I am not sure what a good name would be that clearly indicates it does not return an iterator)

If Some is returned, it replaces the original value; if None is returned, the value is drained. This allows the filter to take ownership of T and only return it if not drained.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing Vec::drain_filter API and the proposed drain_filter_take signature in this issue. A complete contribution would need the ownership and replacement semantics, method naming, and API design to be resolved before implementation; no files or tests are identified in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.