Skip to content

Add method for consuming a Vec and taking one element. #1512

@canndrew

Description

@canndrew

Sometimes I want to take a single element from a Vec and throw the rest of the Vec away. I can do this using swap_remove then dropping the Vec (or letting it fall out of scope) but it would be clearer and slightly more efficient if there was a method for doing this directly.

I propose adding this method to Vec:

impl<T> Vec<T> {
    /// Take a single element of a vector and return it, consuming the
    /// vector. If `index` is invalid the original vector is returned.
    pub fn into_element(self, index: usize) -> Result<T, Vec<T>> {
        ...
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions