News for hackers. Stuff that matters.
Re:C++ vs. Rust Build Times by nextaccountic on Friday January 06, 2023 @06:10PM (#34282701)
> If there are side effects, be procedural.

Yep! That's why Iterator::for_each https://doc.rust-lang.org/stable/std/iter/trait.Iterator.htm... is more often than not a code smell [0]; it exists mostly for the sake of writing side effects in functional style.

So something like

    something.map(|x| ..).for_each(|x| do_something(x));
Should probably be written

    let iter = something.map(|x| ..);

    for x in iter {
        do_something(x);
    }
It's still okay to use Iterator::map and other iterator combinators, but using for instead of for_each here is probably easier to read.

[0] One exception is in the documentation "In some cases for_each may also be faster than a loop, because it will use internal iteration on adapters like Chain."

There are no comments on this comment yet.