Pre-RFC: Break with value in for/while loops

As alternative to the proposed evaluating for to either () or Some(T) depending on the break t, as both types implement the Default trait:

  • If there is a break t in the for then it should evaluate to the type of t.
  • If the for can end without a break t statement (is this always true?), then the type must implement Default, whose default value is used.
  • If there are non-agreeing breaks then it is a syntax error.
  • If there is no break element we advise type inference to be (), but it could be any Default. E.g., with a for being assigned into a variable and then removing all its break statements.

This should be completely compatible with existent code. I have no idea if it would imply some notable change in the compiler. Does the compiler have a notion of the Default trait?

6 Likes