-
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
Add Iterator::enumerate_from(self, first: usize)
#45833
Conversation
There is precedent for an optional `first`/`start` parameter in Python: https://docs.python.org/3/library/functions.html#enumerate
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
This is covered by std by using zip with a range, (from..) as the range in this case. Zip with range is flexible about index type too. Just so that we have existing solutions to compare with. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for zip. In the code in the example I would write (1..).zip(&a)
instead of a.iter().enumerate_from(1)
.
Perhaps we should instead modify the doc to mention |
The documentation can certainly be added to |
I consider |
Also, |
As to this PR, note that |
So what is the status of this PR @dtolnay? Are there modifications that are needed or is it ready to r+? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to see an RFC written for this, to motivate a little better and explore the alternatives. Some things to consider:
-
How this compares with
(n..).zip(iter)
. There is some justification for whyenumerate_from
is better in AddIterator::enumerate_from(self, first: usize)
#45833 (comment). -
How this compares with
iter.enumerate().map(|(i, v)| (n + i, v))
which solves the conditionally double-ended problem and optimized fold. -
Implications of overflow, which
Enumerate::next_back
can assume never happens AddIterator::enumerate_from(self, first: usize)
#45833 (comment). I think this is covered by the disclaimer under "overflow behavior" in the method doc. -
Whether it is okay to tie the return type to be the same struct type between
enumerate
andenumerate_from
. -
Use cases, beyond just that the equivalent exists in Python.
The space of possible solutions turns out to be larger than I thought, so asking to go through the RFC process sounds reasonable. Use cases are mostly with I had not thought of using |
There is precedent for an optional
first
/start
parameter in Python: https://docs.python.org/3/library/functions.html#enumerate