Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
canndrew opened this issue Feb 25, 2016 · 6 comments
Open

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

canndrew opened this issue Feb 25, 2016 · 6 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@canndrew
Copy link
Contributor

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>> {
        ...
    }
}
@oli-obk
Copy link
Contributor

oli-obk commented Feb 25, 2016

You can do vec.into_iter().nth(index), which is pretty concise already, and due to the fact that the Vec iterator is ExactSizeIterator, you'll even get O(1) on the nth operation.

@canndrew
Copy link
Contributor Author

Aha! I didn't think of that. Although the fact that I didn't think of it is more evidence that this method could be useful.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 25, 2016

Although the fact that I didn't think of it is more evidence that this method could be useful.

It's Rust... The first thing you should be thinking when you want something is: "can an Iterator solve this?".

@canndrew
Copy link
Contributor Author

The first thing I think is "does this type have a method that does this?" And if it doesn't it usually should. Also I don't see what ExactSizeIterator has to do with this although I assume std::vec::IntoIter::nth has the behaviour I want.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 25, 2016

I got confused with C++'s random access iterator

@Stebalien
Copy link
Contributor

Actually, Vec's IntoIter doesn't have an optimized nth because it needs to drop the skipped values. This is totally doable (and would give a significant speedup according to a hastily run benchmark) but no one has stepped up to implement it. See: rust-lang/rust#24214

@nrc nrc added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Aug 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

4 participants