Skip to content
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

Closed
kmcallister opened this issue Jun 5, 2014 · 3 comments · Fixed by #15561
Closed

Delay of iteration side effects is confusing #14666

kmcallister opened this issue Jun 5, 2014 · 3 comments · Fixed by #15561

Comments

@kmcallister
Copy link
Contributor

This code produces no output:

fn main() {
    let xs = &[1u,2u,3u];
    let f = |x: &uint| {
        println!("{:u}", *x);
    };
    xs.iter().map(f);
}

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]?

@huonw huonw added the A-libs label Jun 5, 2014
@huonw
Copy link
Member

huonw commented Jun 15, 2014

Perhaps iterator structs should be #[must_use]?

Seems like a good idea. I wonder if #[must_use] could allow specifying a explanation that is printed with the other warnings, so they could have something like #[must_use="iterator adaptors execute lazily"].

@huonw
Copy link
Member

huonw commented Jul 9, 2014

(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.
@emberian
Copy link
Member

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants