Skip to content

What about a Take trait? #61148

@lachlansneff

Description

@lachlansneff

I saw issue #61129, and thought that it might be a good idea to generalize it.

The prelude could export a new trait called Take.

pub trait Take {
    fn take(&mut self) -> Self;
}

A lot of types could implement it, Vec<T> for example. The take method on Option<T> could be moved into this trait, etc.

The Take trait could even be implemented for every type that implements Default:

impl<T> Take for T
where
    T: Default
{
    fn take(&mut self) -> Self {
        mem::replace(self, Default::default())
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions