4 ms·
> In all the languages I know of, you can't break out of a loop from an inner loop. Rust has this feature: https://doc.rust-lang.org/rust-by-example/flow_contr
by Georgelemental 5y ago
> In all the languages I know of, you can't break out of a loop from an inner loop.
Rust has this feature: https://doc.rust-lang.org/rust-by-example/flow_control/loop/nested.html https://doc.rust-lang.org/rust-by-example/flow_control/loop/...
- Jtsummers 5y agoAs do Java, Common Lisp, Ada, Javascript, and probably a lot of others. Breaking from inner loops is a common thing and having to use special condition variables is error prone, in comparison, and potentially more time intensive. Labeled loops let you keep the premise of goto-less programming in structured programming while permitting a weakened jump-to-label (the program can't jump from anywhere to that label, only in the context of the loop). Since they aren't (usually) able to be used from other arbitrary points you still get the cleaner control flow that structured programming promotes.
- ratrocket 5y agoI was writing a (less informed!) sibling comment at the same time. Great observation that I hadn't considered: what I called a "labeled break" or "continue" is safer than bare goto since you can't jump from anywhere to anywhere. Thanks for making that plain!
- ratrocket 5y agoUnless I'm misunderstanding the mechanism (jumping to a label, right?), I thought most (well, lots of) languages supported this. Go does also: A non-minimal example using continue (but you can also break): https://go.dev/play/p/mEyEqcEYvcK https://go.dev/play/p/mEyEqcEYvcK The spec: https://go.dev/ref/spec#Labeled_statements https://go.dev/ref/spec#Labeled_statements It's just goto..? And despite "considered harmful", lots of languages have goto. There are some things for which goto (or labeled break/continue) really are the best way to do what you need. (I can't shake the feeling I'm missing something really obvious here!)
- jibalt 5y agoAll of the languages he mentioned (Rust, Zig, Myrddin) do. I ignored the comparisons to C ... any more modern language looks advanced in comparison.
- deleted 5y ago[deleted]