Comment

Comment by justinpombrio

This is a pretty low quality rant. Going example by example:

    for (i, x) in something.iter_mut().filter(|| {...}).enumerate() {
        *x = (*x) * i
    }
you can this this in loop style if you want:

    for i in range(something.len()) {
        let x = &mut something[i];
        if ... {
            *x = (*x) * i;
        }
    }
or if you prefer, in iter style:

    something
      .iter_mut()
      .filter(|| {...})
      .enumerate()
      .for_each(|(x, i)| *x = (*x) * i);
The next two examples can be written:

    let res: Vec<_> = iterator.iter().filter_map(|x| x.foo()).collect();
    let z = x.foo().and_then(|y| y.bar());
There is an ergonomics issue around the fact that if you stick `?` or `.await()` in a closure, it returns to that closure instead of the function containing it. But the way this issue manifests itself is the standard library having a pile of different variations on methods, like `.map()` vs. `.filter_map()`. That's the thing to complain about, not the fact that closures work the same way they do in every other language.

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton