-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[Language] for-else and while-else #3361
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
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 |
For-else (with Python semantics) implemented as a macro: https://github.com/erkinalp/pythonic-for |
I have updated this proposal, please, read it again |
Isn't this proposal the same as Python's semantics, except that the
(formatting copied from source) Here's an approximate port of your example to Python, which gives the same result: Try it online! for stop_condition in [3, 90]:
print("stop_condition =", stop_condition)
for counter in range(5):
if counter == stop_condition:
result = counter
print("breaking at", counter)
break
else:
result = -7
print("in else clause")
print("The result is", result)
print() Output:
|
with label-break-value this is supported as: let result = 'result: {
for counter in 0..5 {
if counter == 3 {
break 'result counter;
}
}
-7
}; Pro: Need to explain how the |
Uh oh!
There was an error while loading. Please reload this page.
when assigning to a variable,
break
infor
andwhile
isn't allowed, this proposal allow it using aelse
when
break
is not reached, evaluates to value inelse
semantic similar to Zig
https://ziglang.org/documentation/master/#for
read the comment:
Example:
Alternatives
https://rust-lang.github.io/rfcs/2046-label-break-value.html
Alternatives to else keyword
IDK
The text was updated successfully, but these errors were encountered: