Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upif-let patterns should be as expressive as match patterns #935
Comments
This comment has been minimized.
This comment has been minimized.
|
Actually this relates to another issue: Why don't we allow alternations ( |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@lifthrasiir It certainly does, but I feel like many of the issues that pertain to #99 don't apply here, since |
This comment has been minimized.
This comment has been minimized.
|
I guess the only changes we need here are to allow | in the grammar and extend |
This comment has been minimized.
This comment has been minimized.
|
My bad, we'll need to make |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
I don't think |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
Yep. Another reason to avoid it :) |
This comment has been minimized.
This comment has been minimized.
|
I've wanted guards in For some reason |
This comment has been minimized.
This comment has been minimized.
|
@pcwalton: |
This comment has been minimized.
This comment has been minimized.
|
#929 is similar to this bug but it focuses on on adding |
This comment has been minimized.
This comment has been minimized.
|
My last comment was a mistake. They do look slightly similar but they are completely different types of proposals. |
This comment has been minimized.
This comment has been minimized.
camlorn
commented
Apr 23, 2016
|
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. |
This comment has been minimized.
This comment has been minimized.
icorderi
commented
May 24, 2016
|
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: |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
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" notifications@github.com wrote:
|
This comment has been minimized.
This comment has been minimized.
|
@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. |
nrc
added
the
T-lang
label
Aug 29, 2016
This comment has been minimized.
This comment has been minimized.
|
I the merged RFC #2175 covers this issue; so I'm closing it. |
Munksgaard commentedMar 4, 2015
Right now, you can do this
but not this
The same goes for pattern guards.
The RFC for if-let proposes the
if-letnotation pretty much as syntactic sugar for a match block with an empty arm at the end. However,if-letdoesn't allow matching on pattern guards and multiple patterns. Extending theif-letnotation 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 = 0looks weird at best.On the positive side, adding support for guards and multiple patterns shouldn't break anything.
cc @Manishearth