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 Iterator::into_vec() #40051

Closed
HyeonuPark opened this Issue Feb 23, 2017 · 7 comments

Comments

Projects
None yet
5 participants
@HyeonuPark
Copy link

HyeonuPark commented Feb 23, 2017

Though we have nice Iterator::collect(), the most common use cases of this method as I've seen are my_iter.collect::Vec<_>(), which is a bit complicated to type. So it would be worth to add this simple shortcut method for it.

#[inline]
fn into_vec(self) -> Vec<Self::Item> {
    self.collect::<Vec<_>>()
}
@leonardo-m

This comment has been minimized.

Copy link

leonardo-m commented Feb 23, 2017

Is the "to_vec" name better (and shorter)?

@Ixrec

This comment has been minimized.

Copy link
Contributor

Ixrec commented Feb 24, 2017

The other container I very frequently see collect targeting is String, so we may also want an into_string or to_string method.

Whether or not we do that, I'd prefer the into names, since into_* implies a conversion that consumes/destroys/moves the original value (which iiuc is exactly what collect does), while to_* implies some amount of copying is involved so that the original value remains intact. In particular, slices/strs already have to_vec/string and into_vec/string methods with exactly these semantics.

@F001

This comment has been minimized.

Copy link
Contributor

F001 commented Feb 24, 2017

I think this can be done in your own crate. The motivation of adding it to std is not strong enough.

@leonardo-m

This comment has been minimized.

Copy link

leonardo-m commented Feb 24, 2017

If your program is short, doing this in your own crate is overkill.

I think it's a good idea to have a standard method like this because .collect::<Vec<_>>() is long, very common in iterators-based code, not handy to write, quite visually noisy, not easy to discover and remember for newbies.

.into_vec() is visually only one (or two) chunks, compared to .collect::<Vec<_>>(). In D language you can convert a range to an array with just ".array".

Sometimes you can replace .collect::<Vec<_>>() with .collect() and assign the result to a variable that's typed with :Vec<_>, but in longer iterators chains this can't be done.

The need for conversion to a vector is very common (but once the impl trait feature becomes stable, more functions will return an iterator, so some usages of .collect::<Vec<_>>() will be removed).

The itertools crate has this function, named ".collect_vec()":

https://docs.rs/itertools/0.5.9/itertools/trait.Itertools.html#method.collect_vec

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Feb 24, 2017

collect_vec seems to be one of the more popular methods in itertools, if popularity feelings say anything. Best methods from itertools should move into libstd somehow.

Note that Iterator can't include a method called collect_vec or to_vec, because Iterator is in libcore.

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Feb 24, 2017

Wishes for new features (things that are not defects) are filed in the rust-lang/rfcs repo.

I think the most successful way to get a new feature into Rust would be something like:

  1. Sound it out in the https://internals.rust-lang.org/ forum. They will know if a PR or RFC is the best way to go. For RFCs I really recommend posting them as Pre-RFC threads on internals before filing the real thing.
  2. File appropriate PR or RFC

I'll close this issue with that information.

@bluss bluss closed this Feb 24, 2017

@HyeonuPark

This comment has been minimized.

Copy link
Author

HyeonuPark commented Feb 24, 2017

This topic is moved to this thread. Thank you for letting me know the right place to post it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.