-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Description
when assigning to a variable, break
in for
and while
isn't allowed, this proposal allow it using a else
-
when
break
is not reached, evaluates to value inelse
-
semantic similar to Zig
https://ziglang.org/documentation/master/#for
read the comment:
// For loops can also be used as expressions.
// Similar to while loops, when you break from a for loop, the else branch is not evaluated.
- semantic different of Python
Example:
fn main() {
let result = for counter in 0..5 {
if counter == 3 {
break counter
}
} else { -7 };
println!("The result is {result}") // 3
}
fn main() {
let result = for counter in 0..5 {
if counter == 90 {
break counter
}
} else { -7 };
println!("The result is {result}") // -7
}
Alternatives
https://rust-lang.github.io/rfcs/2046-label-break-value.html
Alternatives to else keyword
IDK
Demiu, co42, zjsec, musjj, iago-lito and 10 morelebensterben, xmh0511, AndreiSva, ibraheemdev, SadiinsoSnowfall and 13 more
Metadata
Metadata
Assignees
Labels
No labels