4 ms·
> It is not something that gives you more power, just something that makes code a bit easier to write. Who needs a for loop when you have while?
by bigbillheck 4y ago
> It is not something that gives you more power, just something that makes code a bit easier to write.
Who needs a for loop when you have while?
- dgb23 4y agoFor and while have different semantics. For is not just sugar for a while loop, it means something more specific and restricted than a general while.
- jeffparsons 4y agoFor _is_ "just sugar for a while loop". In fact, here's the Rust documentation detailing what a `for` loop "de-sugars" down to: http://doc.rust-lang.org/nightly/std/iter/#for-loops-and-intoiterator http://doc.rust-lang.org/nightly/std/iter/#for-loops-and-int... > it means something more specific and restricted than a general while Sure, but that's still sugar. That's what sugar is. Want to know something really crazy? In Rust, the `if` keyword is also just syntactic sugar over `match`. Still finding bits of grey matter on the walls from when I learned that one.
- jeffparsons 4y ago`while`? That's just sugar on top of `loop` that automatically inserts an `if` and a `break` for you. Don't be lazy, spell it out. (Although, in Rust, `if` is also actually just sugar over `match`, so, uh...)