Edit, Hmm, Sorry, it has been a few years since I read the online book and I didn't encounter this issue for years because I usually deal with more elegant code, so I asked an AI what the solution would be (to break out of a deeply nested tree of if statements), and it didn't know you can label any scope in rust. Seems like the AI tools are getting worse. I still have to remind them to put the # for #uman readable for elvis.
Thanks @quinedot, It is nice we can label any scope in rust.
Original:
It would be nice if we could label any scope, and then break from them. Similar to (Break with label), but with a better example: I had a complicated set of nested if branching and wanted to break out of them
'my_label : if my_bool
{ // ... do stuff to see if we need to do something else instead
if stop_this_stuff
{ break 'my_label;
}
}
it should be equivalent to :
'my_label : loop
{ if my_bool
{ // ... do stuff to see if we need to do something else instead
if stop_this_stuff
{ break 'my_label;
}
}
break 'my_loop;
}
(which is the current workaround, I also wrote a macro to generate this, but it looks too ugly)
I guess I could just turn my if-conditions into while-loops that break at the end of the loop. But implementing labels for 'if' statements should be incredibly straightforward for compiler developers, given the equivalence with other syntax.