-
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
RFC: or-patterns in let and if / while let expressions #2175
Conversation
This change was also discussed in #2144. |
Looks like a stripped-down version of #1882, which was recently postponed. This RFC adds significantly less new stuff though, since |
@petrochenkov I wouldn't say this and #1882 are related tho. This only makes |
I just saw this RFC in TWIR and funnily enough, a related crate was nominated for crate of the week: https://docs.rs/if_chain/0.1.2/if_chain/#multiple-patterns |
@nugend Interesting! Perhaps worthy of mention in motivation, and possibly alternatives. |
It'd be cool to also have this in plain-old #[derive(Copy, Clone)]
enum Foo {
Bar(i32),
Baz(i32),
}
impl Foo {
fn inner(&self) -> i32 {
let Bar(x) | Baz(x) = *self;
x
}
} |
@cramertj I'm not opposed to this.. But is that a common enough use case tho? Do you have some real world examples I could work into the motivation perhaps? |
@Centril I'm not sure how common it is-- it also only saves a few characters compared to a match. For me, the motivation would be primarily about making patterns consistent. |
@cramertj I guess I buy the consistency argument since I use it under If anyone finds one, please let us know =) |
@Centril There're some functions like the example I gave in rustc, but usually they have one or two variants that don't bind the variable and // Instead of this:
#[derive(Copy, Clone)]
enum Foo {
Bar(i32),
Baz(i32),
}
impl Foo {
fn inner(&self) -> i32 {
let Bar(x) | Baz(x) = *self;
x
}
}
// People write this:
struct Foo {
val: i32,
kind: FooKind,
}
enum FooKind { Bar, Baz } |
@cramertj Right. And isn't that the recommended way of writing it? Devil's advocate: Wouldn't we risk encouraging the "wrong" way of writing this by allowing: let Bar(x) | Baz(x) = *self; ? |
@Centril Yeah, that seems like a reasonable objection. |
@cramertj Hmm.. Perhaps this can be made into a separate RFC since it might be more controversial? - so that this RFC can be merged faster (tho we have several months to discuss it before the impl period is over...) |
@cramertj |
My take is that we definitely want this in I have definitely encountered cases where I want to do:
An example would be in Chalk, where I have this enum: enum ParameterKind<T, L = T> {
Ty(T),
Lifetime(L),
} indicating e.g. the value of a parameter in a list like impl<T> ParameterKind<T> {
fn into_inner(self) -> T { ... }
} there are few times that I wished I could use It doesn't come up a lot, but as @cramertj says, to me having the syntax of |
Nominating for discussin @rust-lang/lang meeting. Seems like one of those "tiny extension to the syntax" sorts of RFCs that are always hard to judge. Obviously not a major blocker for anyone, but maybe a useful thing to go ahead and do? This seems to have pretty minimal implementation cost, I would think, since everything winds up desugared in the compiler to a |
That's an interesting use case! And I agree that diverging would be unfortunate. |
@Centril I say fix it in the RFC now =) |
@rfcbot fcp merge Subjection to the proviso that the RFC is amended to permit |
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@nikomatsakis Amended =) |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
This RFC has been merged! |
This seems quite unfortunate to me that this landed. Other forms of sugar around |
@nox IMO it's a language design bug that |
@eddyb Mmmh, I guess that makes sense if you frame it this way. Thanks for the answer. |
Rendered.
Tracking issue
Enables the use of or-patterns in
let
,if let
, andwhile let
constructs as in:Acknowledgements & ping