-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add Iterator::single #55355
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add Iterator::single #55355
Conversation
Docs should probably mention that in the too-many-items case, two elements are consumed by the Iterator. Actually, it can probably be overridden by Prior art: C#: Enumerable.Single |
Hmm... I debated with myself whether I should include such a note but ultimately I decided it was "too much information" / detail; but I don't feel strongly. Do you have some good phrasing I copy into the docs perhaps?
Thanks! |
I'm really not a fan of making "0 elements" and "more than one element" indistinguishable. I feel like the whole point of |
If by "coding error" you mean logic bug, then I disagree. In the cases (proc macros) I wanted
Idk what that means... elaborate? I feel strongly that the |
Seems silly, but why not |
Actually, it would be |
@clarcharr
That's not so bad actually... I could be convinced in favor of it since it doesn't invent new types...
So |
I'm not sure how best the docs could mention that two elements of the iterator are consumed. But I'm pretty certain that most uses of this fn would be when the author is reasonably certain that the iterator should have a single element, and otherwise is a logic error or input error. There is benefit in returning the second item in the most common case I've seen for In conclusion, I have confused myself out of what would be the ideal, canonical signature for this fn. I think I lean very weakly towards |
I guess we could just say "the method consumes at most two elements from the iterator and does not exhaustively consume the entire iterator" or something.
Logic errors or input errors are very different and shouldn't be handled the same way.
I buy that I guess; I could go with Should we keep it as is?, change it?, wait for someone on T-libs before landing? |
cc @alexcrichton any chance you might get to this (there are some minor open design questions here for you to resolve...)? |
Er well I don't think I'm personally on the hook to solve any design questions here. If this isn't ready to land then I think we should close this PR until those design questions are fleshed out, or they can be hashed out on thread as well. My personal gut reaction to this method is that it's not worth it to add to the standard library, so I don't really have many opinions about the precise pieces here. If signatures like |
Eh well... The language team is on the hook to resolve (not solve) design questions about the language so it's not too crazy for y'all to resolve libs questions and pick between designs. :)
Perhaps we can land the PR as-is and get some experience on nightly then?
Can you elaborate on why perhaps? I'd like to note that numerous people noted that this method was useful to them on internals (see https://internals.rust-lang.org/t/what-do-you-think-about-iterator-single/8608).
OK but then it seems like you do have some opinions about the design (namely that the signature as-is in the PR belongs more in libcore than other signatures?). |
@Centril you singled me out specifically as specifically getting to this and solving open design problems here. It's not really my job, nor the libs team's, to solve any arbitrary bikeshed presented to them. We're all busy folks and are balancing contributing and reviewing with other tasks, and if something doesn't grab anyone's attention or doesn't seem like it should go in libstd, then it ends up not doing so. Modifying core traits like Iterators are used by practically everyone who uses Rust, so no matter what method you propose for an iterator some subset of folks will want to see it included in libstd and standardized. To me, however, this seems like very niche functionality where you want a guarantee that there's only one element in the iterator rather than just the next element. Start talking about recovering the iterator and item in erroneous cases and that's just way oer the top for me to be included in libstd. |
Tracking issue: #55354
Suggested and discussed in https://internals.rust-lang.org/t/what-do-you-think-about-iterator-single/8608.
Alternatives
Designs
Alternative designs could use a dedicated enum:
but I think the common case doesn't need that.
This addition is also cheaper complexity wise this way.
Bikeshed
I picked
single
since it strikes a balance between brevity and clarity.r? @alexcrichton