LLM tools degrading, couldn't answer simple question - labels for non-loop scopes

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.

Labeled bare blocks are supported.

-    'my_label: loop {
+    'my_label: {
         if my_bool {
             if stop_this_stuff {
                 break 'my_label;
             }
         }
-        break 'my_label;
     }

In prior discussions, I recall some disagreement on if a breaking if would mean "jump to the next else if conditional or else block" or "jump beyond the entire chain", which probably means that such a feature would be confusing to one camp or another.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.