rust-lang / rust-lang/rfcs

Add method for consuming a Vec and taking one element.

Open
#1,512 6 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

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>> {
        ...
    }
}

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 reviewing the proposed Vec::into_element signature and the six-comment discussion. The payload identifies no implementation files or tests, so first determine the applicable RFC process and affected Rust API area. Done would require resolving the proposal's design and documenting an accepted decision.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.