3 ms·
> For loops are not idiomatic in Rust That's not really the case. For loops are certainly less common compared to C, but they are completely fine to use. Forci
by dist1ll 2y ago
> For loops are not idiomatic in Rust
That's not really the case. For loops are certainly less common compared to C, but they are completely fine to use. Forcing every loop into an iterator-chain style can sometimes be more confusing.
Not to mention doing for-iterations with early exits. Yes, you can use try_fold, but if I'm not writing generic iterator library code, I'd prefer explicit for in cases like this.
> writing Fortran-ey code instead of iterators will lead to pain and slower loops.
No it won't. At least not in the general case. Not to mention that iterators also have performance footguns that are pretty easy to trigger.
- John23832 2y ago>That's not really the case. For loops are certainly less common compared to C, but they are completely fine to use. Forcing every loop into an iterator-chain style can quickly become an unreadable mess. There are no C style "for" loops in Rust: https://doc.rust-lang.org/reference/expressions/loop-expr.html https://doc.rust-lang.org/reference/expressions/loop-expr.ht... There is the "for" expression, but that does not give you a C-style "for" loop. It gives you something akin to a "foreach" you might see in Java, C#, Go, etc.
- steveklabnik 2y agoFrom the link: > A for expression extracts values from an iterator, looping until the iterator is empty. They look like this: for i in 1..10 { println!("{i}"); } And they're totally fine to use in Rust, I agree that the article is not exactly accurate here. EDIT: Okay you edited your comment to say this doesn't count. Sure. This is what the author of the post was talking about, though.
- John23832 2y agoI guess the statement >>That's not really the case. For loops are certainly less common compared to C, but they are completely fine to use. Forcing every loop into an iterator-chain style can quickly become an unreadable mess. Implies a C-style for loop, some sort of counting accumulator, and an update expression. That doesn't exist in rust. That's all I was saying. I know who you are, so I know I'm preaching to the preacher.
- steveklabnik 2y agoGotcha. I don't think it implies a C-style for loop, guess we just read things differently :)
- bee_rider 2y agoThe author of the post says: > For loops are not idiomatic in Rust - writing Fortran-ey code instead of iterators will lead to pain and slower loops. Spending time reading the examples in the iterator docs and the community solutions in the exercisms will help a lot. I think they are talking about C style for loops or Fortran style do loops, they explicitly contrast against iterators. Edit: Although, I wonder, what does an iterator like you’ve shown compile down to? I’d imagine Rust is smart/modern enough and that iterator is common enough that the assembly emitted looks a lot like a c for loop. Or better.
- steveklabnik 2y agoI understood them to be saying that they would prefer (0..10).iter().for_each(|x| println!("{x}")) over the thing that I wrote. Many folks do not think of Rust's for loops as using iterators under the hood, and so will often talk about them as though they don't use iterators, even though they technically do.
- miguelraz 2y agoAuthor here, long time no see Steve! When I wrote this, my main takeaways were numerical computations with arrays, which don't tend to use for loops in Rust, and which require grokking iterators for maximum performance and rayon-parallelism boons. To a Julia audience, seeing this style of iterator chaining is quite a new flavor to think about composing computation, so I wanted it was with that audience in mind that I wrote this.
- steveklabnik 2y agoHey hey! Yeah I think that makes sense! I don't do that kind of work and so it's more usual in the kinds of code I write.
- dist1ll 2y agoIIRC for loops compile down to a while loop with the same body, that drives the given iterator with .next(), until it returns None. Since the .next() implementation of std::ops::Range is essentially an i++, you'll quickly arrive at a similar representation as classic C for-loop.
- kstrauser 2y agoThat's nearly identical to Python's approach, too. It doesn't have C-style loops, just ones that consumer iterators. for i in range(10): print(i) consumes 1 value at a time from the `range(10)` generator. `for` has no special knowledge of numbers or ranges or any of that.
- dist1ll 2y agoI think that's bit too pedantic. Most people in the Rust community refer to for expressions as for loops (including the book). It's especially odd to point out this language detail, because for expressions always return the unit type. So it's not like you can do something with the return value (unlike loop expression, which can evaluate to one of its break expressions).
- John23832 2y agoThe ergonomics of c style for loops and iterator style loops are totally different. It’s the reason for crates like itertools. I don’t think that is pedantic at all.
- dist1ll 2y agoSure, but that's kind of beside the point I was making. The author was contrasting loops - an imperative construct (which both C-style for and iterator-based for are examples of) - to the functional style of chaining iterator functions, adapters & reductions and passing closures around. FWIW I agree with you, it's just that I was trying to make a distinction between imperative and functional style in Rust (just like the author was trying to do) - not literally claiming Rust has C-style for loops.