Skip to content
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

Open
igotfr opened this issue Dec 27, 2022 · 11 comments
Open

[Language] for-else and while-else #3361

igotfr opened this issue Dec 27, 2022 · 11 comments

Comments

@igotfr
Copy link

igotfr commented Dec 27, 2022

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 in else

Example:

fn main() {
  let result = for counter in 0..5 {
    if counter == 3 {
      break i
    }
  } else { -7 };

  println!("The result is {result}") // 3
}
fn main() {
  let result = for counter in 0..5 {
    if counter == 90 {
      break i
    }
  } else { -7 };

  println!("The result is {result}") // -7
}

Alternatives

https://rust-lang.github.io/rfcs/2046-label-break-value.html

@PatchMixolydic
Copy link
Contributor

This was previously suggested in #3163, which was closed due to concerns over the clarity of the syntax.

@igotfr
Copy link
Author

igotfr commented Dec 30, 2022

This was previously suggested in #3163, which was closed due to concerns over the clarity of the syntax.

when break is not reached, evaluates to value in else, what's the struggle?

@SOF3
Copy link

SOF3 commented Dec 31, 2022

@cindRoberta the behavior in most other languages only runs else if no iterations happened at all, no matter break or not

@igotfr
Copy link
Author

igotfr commented Dec 31, 2022

@cindRoberta the behavior in most other languages only runs else if no iterations happened at all, no matter break or not

@SOF3 in other languages, for and while aren't expressions. It's a weird behaviour for and while ever return (), the behaviour that I proposed is the same of Zig, Zig uses else

I proposed a different behaviour than Python

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.

I'm not creating nothing, it already exists

@SOF3
Copy link

SOF3 commented Jan 1, 2023

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 case without break not falling through in golang because everyone is used to that fact.

@igotfr
Copy link
Author

igotfr commented Jan 3, 2023

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 case without break not falling through in golang because everyone is used to that fact.

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

@SOF3
Copy link

SOF3 commented Jan 4, 2023

But what you can do with for-else can already be done with iterators right? break-else is basically the same as iter.find_map(...).unwrap_or_else(...).

@igotfr
Copy link
Author

igotfr commented Jan 4, 2023

But what you can do with for-else can already be done with iterators right? break-else is basically the same as iter.find_map(...).unwrap_or_else(...).

@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 ()

@safinaskar
Copy link

@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

@igotfr
Copy link
Author

igotfr commented Feb 11, 2023

@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

@igotfr igotfr changed the title for-else and while-else [Compiler] for-else and while-else Feb 26, 2023
@igotfr igotfr changed the title [Compiler] for-else and while-else [Language] for-else and while-else Feb 26, 2023
@sollyucko
Copy link

Here's are the relevant links I've found:

Previously proposed in:

Alternative features and proposals:

Similar features or proposals in other languages with the semantics you're proposing:

Features or proposals with similar syntax but semantics to run the else if the loop never iterates:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants