Description
The reason I'm opening this issue is that there has been some activity on the old issues, regarding returning values with break
from for
and while
loops:
- Allow loops to return values other than () #961
- Allow for and while loop to return value (without discussion of keyword names) #1767
Rereading those threads, the main issues why those were originally closed seem to be:
else
being a confusing name for this block- not being able to add a new keyword that makes the meaning of the block clear
- there was no experience with the
loop
version of thebreak <value>
syntax
Number 1 is still valid (although some disagree). 3 is not valid anymore, this feature was stabilized more than 4 years ago in 2017. Number 2 seems much less of a problem these days, since we now have editions (which we didn't have when these original issues were opened).
That's why I'm proposing to reserve nobreak
as a keyword in Rust Edition 2021. That way we at can add this feature in the future, using a clear keyword.
I think nobreak
clearly shows the meaning of the block. It's the block that you enter when there was no break. Code would then look like this:
let found_six = for val in [1, 4, 3, 2] {
if val == 6 {
break true;
}
} nobreak {
false
}