-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Delay of iteration side effects is confusing #14666
Comments
Seems like a good idea. I wonder if |
(I'm working on this.) |
huonw
added a commit
to huonw/rust
that referenced
this issue
Jul 9, 2014
It can be a little unintuitive that something like `v.iter().map(|x| println!("{}", x));` does nothing: the majority of the iterator adaptors are lazy and do not execute anything until something calls `next`, e.g. a `for` loop, `collect`, `fold`, etc. The majority of such errors can be seen by someone writing something like the above, i.e. just calling an iterator adaptor and doing nothing with it (and doing this is certainly useless), so we can co-opt the `must_use` lint, using the message functionality to give a hint to the reason why. Fixes rust-lang#14666.
bors
added a commit
that referenced
this issue
Jul 10, 2014
Similar to the stability attributes, a type annotated with `#[must_use = "informative snippet"]` will print the normal warning message along with "informative snippet". This allows the type author to provide some guidance about why the type should be used. --- It can be a little unintuitive that something like `v.iter().map(|x| println!("{}", x));` does nothing: the majority of the iterator adaptors are lazy and do not execute anything until something calls `next`, e.g. a `for` loop, `collect`, `fold`, etc. The majority of such errors can be seen by someone writing something like the above, i.e. just calling an iterator adaptor and doing nothing with it (and doing this is certainly useless), so we can co-opt the `must_use` lint, using the message functionality to give a hint to the reason why. Fixes #14666.
This just saved my bacon, and is SUPER useful. ❤️ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code produces no output:
It's because the iterator returned by
map
is never used. Switching to.map(f).last()
produces the expected output.Perhaps iterator structs should be
#[must_use]
?The text was updated successfully, but these errors were encountered: