-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Tracking Issue for Iterator::collect_into
#94780
Comments
This would be a useful method when using |
Not sure if this is where any discussion should go but I would like it to formally request from now that consideration should be given to the ability to (try to?) collect into a fixed-size buffer well, primarily for purposes of avoiding heap allocation. Presumably this would just be a separate feature (
The idea is that sometimes you want to use the iterator façade to simplify code transforming an existing collection and your input set is either of a known size or a capped size. Being able to collect into a stack-allocated array or mutable slice thereof would allow collecting the results of an enumeration cleanly. The semantics can (but don't necessarily have to) differ from those of You can currently mock this yourself by implementing EDIT: It may very well just make more sense to loop over the items and add them to the array rather than building a huge façade around that. (Technically the same goes for |
Regarding the last point:
I have an example use case, and would like to know how it should be written differently without this API: I'm implementing a limited size priority queue (i.e. I want to keep only the top N elements of the queue at any given time).
How would it look with an alternative API? |
The two codes are equivalent (in fact, |
Yeah, I think it should be. However, seeing that
I don't think it will really impact
That would probably just be the implementation of |
Feature gate:
#![feature(iter_collect_into)]
This is a tracking issue for adding the
collect_into
method to theIterator
trait.Iterator::collect_into
lets an iterator to be collected into a collection which implements theExtend
trait, consuming the iterator and adding every of its item to the collection.Adding this method has also the benefit of making the
Extend
trait more discoverable.Public API
Steps / History
Unresolved Questions
Iterator
interface is already pretty large, and use cases can easily be written differently without this API.The text was updated successfully, but these errors were encountered: