-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Language] for-else and while-else #3361
Comments
This was previously suggested in #3163, which was closed due to concerns over the clarity of the syntax. |
when |
@cindRoberta the behavior in most other languages only runs |
@SOF3 in other languages, I proposed a different behaviour than Python https://ziglang.org/documentation/master/#for read the comment:
I'm not creating nothing, it already exists |
Yes, but that's exactly the reason why it is extremely confusing if someone ends up using it as a non-expression. This is just as confusing as |
if, for, while and loop in Rust are ever evaluating for a value, same when not binded to a variable. Just that for and while are ever evaluating to () and if and loop not if the struggle is the confusion with Python, use other keyword than else |
But what you can do with for-else can already be done with iterators right? break-else is basically the same as |
@safinaskar what can to be done with for-else can already be done too with labels, for-else is just fill a gap of ever evaluate to () |
@cindRoberta , what you want is already possible thanks to recently stabilized "break from labeled blocks" ( see https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html ). Here is how we can write your example with recent stable Rust: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4981da6fe916fc970922ebcd8e639e2c |
https://rust-lang.github.io/rfcs/2046-label-break-value.html |
when assigning to a variable,
break
infor
andwhile
isn't allowed, this proposal allow it using aelse
break
is not reached, evaluates to value inelse
Example:
Alternatives
https://rust-lang.github.io/rfcs/2046-label-break-value.html
The text was updated successfully, but these errors were encountered: