-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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::collect_into #2339
Comments
@CodeSandwich : Thanks for opening this! An example of the usecase from rust-lang/rust#45840 would be nice to see: ie., just using
|
|
We could simply name the method I don't think this requires an RFC. You can just file a PR against rust-lang/rust. |
Is it allowed to use collect_into on an fixed-size array? |
Arrays do not implement |
Arrays of |
Often I have to fill fixed-size arrays. Sometimes I'd like a syntax like: let a = vec![|x| (x as u32).pow(2); 9]; |
Smells like Python.
The arrays are way more restricted (often just useless) because of safety constraints. If the closure panics half way through initialization, Rust will not be able to drop it properly. The workaround could be ArrayVec. |
I've hit a usability wall during implementation. When using the method:
Compiler can't infer the type of the
Rust cant' find the right combination of
There are two solutions:
|
Why is the |
It's useful when you want to pass |
@CodeSandwich I'd say that specifying |
@Centril Thank you, I split If this change doesn't have own RFC, should I already mark it as |
The additions to the standard library always start off as unstable. For features that haven’t gone through the RFC process, pnce the implementation is nearing completion (the PR is submitted and mostly reviewed), a tracking issue is created and unstable annotations are pointed at the newly created tracking issue. |
Set the issue number to 0 for now and no since attribute. Once the libs team member asks you, create a new tracking issue and make a commit to the PR and set an issue number. |
Closing since the PR was filed. |
Add Iterator::collect_into This is PR for adding `Iterator::extend` and `Iterator::extend_mut` as discussed in [RFC issue 2339](rust-lang/rfcs#2339). It was advised to bypass RFC and just implement it.
Summary
Add
Iterator::collect_into
method, which takes a collection and extends it with iterator’s contentMotivation
Ergonomics
Sometimes there is a need to create a sophisticated iterator and then put all it's items into a collection. Of cource there is
Iterator::collect
, which creates a new collection, but there is no convenient method of collecting into an EXISTING one.Sure, it's possible, but a bit awkward:
or more compact, but way harder to follow:
but nothing beats THIS:
collect_into
could return the extended collection:the function could accept collections by both reference or value:
Manually size-hinted
collect
This use case was invented by @stuhood in Rust issue 45840.
As originally determined in https://twitter.com/kot_2010/status/927119253324619776), building a pre-size-hinted mutable
Vec
as the output of a loop (even for very smalln
) can occasionally be more than twice as fast as collecting into aVec
. As demonstrated here , using extend into a pre-size-hintedVec
closes most of the gap.Focusing on improving
Iterator::size_hint
implementations so that they suggest better sizes for collect would likely help reduce this gap. But there are diminishing returns to heuristics, and in many cases users should be able to give better explicit hints.Guide-level explanation
Iterator::collect_into
takes all items from theIterator
and adds them to an existing collection. It's a counter-part forExtend::extend
, but instead of being invoked on container, it's called on Iterator making it's usage similar toIterator::collect
.Iterator::collect_into
takes collection either by mutable reference or value. It also returns the extended collection making chaining elegant.Reference-level explanation
No rocket science here.
The
BorrowMut
allows passing collections either by value or by reference, which makes it much more elastic.Drawbacks
This proposition makes
Iterator
interface even broader without extending its functionality significantly, it's just ergonomics.Rationale and alternatives
This proposition removes a small
stdlib
papercut making it more pleasant and expressive. If it's not added, people will just keep using workarounds withExtend::extend
.Unresolved questions
None, that I'm aware of.
The text was updated successfully, but these errors were encountered: