-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Closed
Labels
C-feature-acceptedCategory: A feature request that has been accepted pending implementation.Category: A feature request that has been accepted pending implementation.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Description
0b_0101_001_1010 had a good idea on Reddit. I'll paraphrase it here.
The only way to eagerly consume an iterator, e.g., for its side-effects, on stable Rust is to call .collect or .count (.for_each is nightly only). Most beginners don't associate .count with this, so they use .collect, which is horrible.
There are a few things we could do to help make it more obvious what the correct thing to do is:
- make
.collect's result#[must_use]. If you are throwing.collect's result away, you are doing it wrong, and should not have used.collectin the first place. A loop or.for_eachwould do. - make
.count's result#[must_use]: while.countis "efficent" (when compared to.collect) it doesn't convey what the code is actually doing - add an
.eval()==.for_each(|_|): because.for_each(|_|)is also ugly - address this in docs:
.collectmeans copy the results of the iterator into a collection, not "eagerly evaluate", or similar
MatthieuBizien and hcplbluss and scottmcm
Metadata
Metadata
Assignees
Labels
C-feature-acceptedCategory: A feature request that has been accepted pending implementation.Category: A feature request that has been accepted pending implementation.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.