rust-lang / rust-lang/rfcs

Alternative to `drain`

Open
#1,433 12 comments 0 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

Right now, a lot of types implement drain without any parameters. Personally, I think that this method should be made part of IntoIterator for these types. Here's an example of what the new trait would look like:

pub trait IntoIterator where Self::IntoIter::Item == Self::Item {
    type Item;
    type IntoIter: Iterator;
    fn into_iter(self) -> Self::IntoIter;

    // additions
    type Drain: Iterator = Self::IntoIter;
    fn drain(&mut self) -> Self::Drain where Self: Default {
        ::std::mem::replace(self, Self::default()).into_iter()
    }
}

Really, drain(..) is just a form of into_iter which reuses resources. Once associated type defaults are no longer gated, we may be able to make this change to the standard library without breaking user code, assuming that we delete the native impl or provide a different name. (although, we'd have to check that)

What do you think?

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 with the proposed IntoIterator trait and the discussion of associated type defaults, native implementation removal, and compatibility. Review the 12-comment thread for unresolved objections; done would require a settled RFC direction that addresses these compatibility concerns.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
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.