Uh oh!
There was an error while loading. Please reload this page.
Enable draining iterator/stream elements into a () - #94166
Conversation
rust-highfive
commented
Feb 19, 2022
r? @yaahc (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This might affect inference in cases like: structBaz;implInto<()>forBaz{fninto(self) -> (){()}}implInto<i32>forBaz{fninto(self) -> i32{0}}fnmain(){let _:() = vec![Baz].into_iter().map(|b| b.into()).collect();} |
| iter.into_iter().for_each(|()| {}) | ||
| impl<T> FromIterator<T> for () { | ||
| fn from_iter<A: IntoIterator<Item = T>>(iter: A) -> Self { | ||
| iter.into_iter().for_each(|_| {}) |
There was a problem hiding this comment.
| iter.into_iter().for_each(|_| {}) | |
| iter.into_iter().for_each(drop) |
Maybe this should also use .for_each(drop) to clarify the intent better? It doesn't matter much for (), but for generic T that seems better.
What is the motivating use case for this PR? To me |
ibraheemdev
commented
Apr 15, 2022
The use case I ran into was running an iterator completion, stopping on the first error, but not caring about the success type. |
I think I understand what you mean but the example you gave is a little confusing. You mentioned stopping on the first error but then the snippet shows a Regardless, In this case I'd still prefer to push people towards the explicit case. I don't think that collecting into cc @scottmcm |
ibraheemdev
commented
Apr 15, 2022
Yes, sorry, I meant |
And in the case where you drop there's no need to collect it into a |
ibraheemdev
commented
Apr 15, 2022
Right, yes, |
compiler-errors
commented
Apr 15, 2022
I still think that breaking inference is a concern, and possibly does not outweigh the convenience of not having to map the elements under drop. I guess we could do crater to check if there are people actually using the |
scottmcm
commented
Apr 19, 2022
I've never really been a fan of this, going back to even when it was first added: #45379 (comment) I can accept it for But when actually throwing stuff away, I think rust/library/core/src/iter/traits/iterator.rs Line 1783 in c102c5c So like #64117 (comment), I'd propose just closing this with reference to #48945 (comment). |
@scottmcm I think this case is slightly different from those because it is simultaneously dropping the iter.try_for_each(|r| {let _ = r?;Ok(())})?;I agree with wanting to close this though, especially given that this is an insta-stable change. @ibraheemdev I appreciate the work you've put into this, but I don't think we should add this change to the standard library. I'm going to close this for now. If additional reasons come up, or additional information changes some of these conclusions, we can always reopen this PR. Regardless, thank you for your work. |
The current implementation of
Extendfor()was added in #50234 because:Making the implementation generic makes something like
stream.collect::<()>possible for any stream, vs.stream.map(drop).collect::<()>.Similarly,
impl<T> FromIterator<T> for ()makesiter.collect::<()>a generic replacement foriter.for_each(drop).