-
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
if-let patterns should be as expressive as match patterns #935
Comments
Actually this relates to another issue: Why don't we allow alternations ( |
@lifthrasiir It certainly does, but I feel like many of the issues that pertain to #99 don't apply here, since |
I guess the only changes we need here are to allow | in the grammar and extend |
My bad, we'll need to make |
This branch adds support for multiple patterns in both if-let and while-let statements. If we want to do the same for guards, we'll probably need to discuss what syntax we want. https://github.com/Munksgaard/rust/tree/if-while-let-multiple-patterns |
I don't think |
I don't really see any other reasons beside the syntax for not letting As you're saying though, the syntax could get really ugly. Wouldn't |
Yep. Another reason to avoid it :) |
I've wanted guards in For some reason |
@pcwalton: |
#929 is similar to this bug but it focuses on on adding |
My last comment was a mistake. They do look slightly similar but they are completely different types of proposals. |
What is the status of this, and is this the latest discussion? I want to talk about this as someone who is new to Rust but has significant experience with C++ and has played with Haskell. Rust sold itself to me before I had even written a line. I read about patterns in the book. Then I read about if let in the book, and it doesn't mention any restrictions. As someone coming into the language, this feels like a bug or oversight, not something that should have an RFC. I do agree that the syntax is ugly, but I don't see it as any less ugly than the corresponding match for the same thing. The if let has the pattern guard, but the match has the second arm, the pattern guard, and an additional level of nesting. If there is some obvious reason that this shouldn't work as one would expect, I'd love to know what it is. A lot of my annoyance with learning Rust has been mitigated because I can look at it and see the obvious reason. But I can't here, and it doesn't exist judging by the other comments on this issue. I would expect that a pattern is always the same thing and following the same rules, not that it's sometimes different. |
I just run into this, was surprised I couldn't use a guard in the if-let. This looks very weird and wouldn't have expected it to compile: But I was expecting the following to work: |
I've run into this several times as well. Can't we just have a single syntax for a "pattern" which includes multiple cases, guards, etc. and is used for |
That sort of pat does not make sense in the context of arguments (e.g. fn On Aug 28, 2016 9:36 PM, "Diggory Blake" [email protected] wrote:
|
@nagisa That's simply the difference between refutable and irrefutable patterns. Function args must of course be irrefutable, unless you allow multiple function definitions to cover all possible cases. |
I the merged RFC #2175 covers this issue; so I'm closing it. |
But |
I have wanted this feature many times, it doesn't make sense for it to not be there. |
I don't understand why this was closed — there are more things missing from |
@Twey issues on the rfcs repo aren't really a "thing" either way, they're used inconsistently and we're moving away from that. Feel free to discuss this on internals.rust-lang.org and open an RFC |
To those that find this from a web search, here's the |
I'm assuming
|
Right now, you can do this
but not this
The same goes for pattern guards.
The RFC for if-let proposes the
if-let
notation pretty much as syntactic sugar for a match block with an empty arm at the end. However,if-let
doesn't allow matching on pattern guards and multiple patterns. Extending theif-let
notation to allow guards and multiple patterns feel like a natural extension to the current functionality with plenty of use cases.I've started on an actual rfc, but there might be some parsing and syntactic details we'd have to figure out before it makes sense to finish it. For instance,
if let Some(n) = x if n = 0
looks weird at best.On the positive side, adding support for guards and multiple patterns shouldn't break anything.
cc @Manishearth
The text was updated successfully, but these errors were encountered: