Loop labels are rare, but they lead to much simpler/clearer code when you need them. Consider how you would implement this kind of loop in a language without loop variables:
'outer: while (...) {
'inner: while (...) {
if (...) {
// this breaks out of the outer loop, not just the inner loopbreak'outer;
}
}
// some code here
}
In C/C++ you’d need to do something like
bool condition = false;
while (...) {
while (...) {
if (...) {
condition = true;
break;
}
}
if (condition) {
break;
}
// some code here
}
Personally, I wouldn’t call it ugly, either, but that’s mostly a matter of taste
Well, you’d typically put the loops into a function and then do an explicit return to jump out of there. I believe, there’s some use-cases where this isn’t possible, which is why I’m cool with loop labels existing, but I’ve been coding Rust for seven years and have not needed them once…
Loop labels are rare, but they lead to much simpler/clearer code when you need them. Consider how you would implement this kind of loop in a language without loop variables:
'outer: while (...) { 'inner: while (...) { if (...) { // this breaks out of the outer loop, not just the inner loop break 'outer; } } // some code here }In C/C++ you’d need to do something like
bool condition = false; while (...) { while (...) { if (...) { condition = true; break; } } if (condition) { break; } // some code here }Personally, I wouldn’t call it ugly, either, but that’s mostly a matter of taste
Well, you’d typically put the loops into a function and then do an explicit
returnto jump out of there. I believe, there’s some use-cases where this isn’t possible, which is why I’m cool with loop labels existing, but I’ve been coding Rust for seven years and have not needed them once…