From 04cc7ea6cdb0441de60c423ba58fe9afdb5ff8b3 Mon Sep 17 00:00:00 2001 From: Philip Munksgaard Date: Wed, 4 Mar 2015 11:09:38 +0100 Subject: [PATCH 1/5] Add if-let_multiple_patterns RFC --- text/0000-if-let_multiple_patterns.md | 81 +++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 text/0000-if-let_multiple_patterns.md diff --git a/text/0000-if-let_multiple_patterns.md b/text/0000-if-let_multiple_patterns.md new file mode 100644 index 00000000000..a7a943ed75c --- /dev/null +++ b/text/0000-if-let_multiple_patterns.md @@ -0,0 +1,81 @@ +- Feature Name: if_let_multiple_patterns +- Start Date: 2015-03-04 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary + +Allow `if let` to match on multiple patterns with `|`. + +# Motivation + +The goal of this RFC is to enable `if-let` to match on multiple patterns, just +like match is able to. + +Currently, the `if-let` notation is inconsistent with the `match` notation, +even though `if-let` is mostly sugaring for `match`. Specifically, `if-let` does not +allow matching on multiple patterns. + +For instance, the following piece of code is allowed: + +```rust +enum Foo { + One(u8), + Two(u8), + Three +} +use Foo::*; + +fn main () { + let x = One(42); + match x { + One(n) | Two(n) => { + println!("Got one or two with val: {}", n); + } + _ => {} + } +} +``` + +but this isn't: + + +```rust +enum Foo { + One(u8), + Two(u8), + Three +} +use Foo::*; + +fn main () { + let x = One(42); + if let One(n) | Two(n) = x { + println!("Got one or two with val: {}", n); + } +} +``` + +This RFC proposes to extend the notation for `if-let` to also cover multiple +patterns separated by pipes (`|`). + +# Detailed design + +[if-let](text/0160-if-let.md) + + +This is the bulk of the RFC. Explain the design in enough detail for somebody familiar +with the language to understand, and for somebody familiar with the compiler to implement. +This should get into specifics and corner-cases, and include examples of how the feature is used. + +# Drawbacks + +Why should we *not* do this? + +# Alternatives + +What other designs have been considered? What is the impact of not doing this? + +# Unresolved questions + +What parts of the design are still TBD? From 31150adf758021286742db1be7c56633fb9743a7 Mon Sep 17 00:00:00 2001 From: Philip Munksgaard Date: Wed, 4 Mar 2015 18:20:06 +0100 Subject: [PATCH 2/5] Update and rename 0000-if-let_multiple_patterns.md to 0000-if-while-let_multiple_patterns.md --- text/0000-if-let_multiple_patterns.md | 81 --------------------- text/0000-if-while-let_multiple_patterns.md | 76 +++++++++++++++++++ 2 files changed, 76 insertions(+), 81 deletions(-) delete mode 100644 text/0000-if-let_multiple_patterns.md create mode 100644 text/0000-if-while-let_multiple_patterns.md diff --git a/text/0000-if-let_multiple_patterns.md b/text/0000-if-let_multiple_patterns.md deleted file mode 100644 index a7a943ed75c..00000000000 --- a/text/0000-if-let_multiple_patterns.md +++ /dev/null @@ -1,81 +0,0 @@ -- Feature Name: if_let_multiple_patterns -- Start Date: 2015-03-04 -- RFC PR: (leave this empty) -- Rust Issue: (leave this empty) - -# Summary - -Allow `if let` to match on multiple patterns with `|`. - -# Motivation - -The goal of this RFC is to enable `if-let` to match on multiple patterns, just -like match is able to. - -Currently, the `if-let` notation is inconsistent with the `match` notation, -even though `if-let` is mostly sugaring for `match`. Specifically, `if-let` does not -allow matching on multiple patterns. - -For instance, the following piece of code is allowed: - -```rust -enum Foo { - One(u8), - Two(u8), - Three -} -use Foo::*; - -fn main () { - let x = One(42); - match x { - One(n) | Two(n) => { - println!("Got one or two with val: {}", n); - } - _ => {} - } -} -``` - -but this isn't: - - -```rust -enum Foo { - One(u8), - Two(u8), - Three -} -use Foo::*; - -fn main () { - let x = One(42); - if let One(n) | Two(n) = x { - println!("Got one or two with val: {}", n); - } -} -``` - -This RFC proposes to extend the notation for `if-let` to also cover multiple -patterns separated by pipes (`|`). - -# Detailed design - -[if-let](text/0160-if-let.md) - - -This is the bulk of the RFC. Explain the design in enough detail for somebody familiar -with the language to understand, and for somebody familiar with the compiler to implement. -This should get into specifics and corner-cases, and include examples of how the feature is used. - -# Drawbacks - -Why should we *not* do this? - -# Alternatives - -What other designs have been considered? What is the impact of not doing this? - -# Unresolved questions - -What parts of the design are still TBD? diff --git a/text/0000-if-while-let_multiple_patterns.md b/text/0000-if-while-let_multiple_patterns.md new file mode 100644 index 00000000000..574f702ec92 --- /dev/null +++ b/text/0000-if-while-let_multiple_patterns.md @@ -0,0 +1,76 @@ +- Feature Name: if_while_let_multiple_patterns +- Start Date: 2015-03-04 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary + +Allow `if let` and `while let` to match on multiple patterns with `|`. + +# Motivation + +The goal of this RFC is to enable `if let` and `while let` statements to match on multiple patterns, just +like `match` is able to. + +Currently, the `if let` and `while let` notation is inconsistent with the `match` notation, +even though they are is just sugaring for `match`. Specifically, `if let` and `while let` do not +allow matching on multiple patterns. + +For instance, the following piece of code is allowed: + +```rust +enum Foo { + One(u8), + Two(u8), + Three +} +use Foo::*; + +fn main () { + let x = One(42); + match x { + One(n) | Two(n) => { + println!("Got one or two with val: {}", n); + } + _ => {} + } +} +``` + +but this isn't: + + +```rust +enum Foo { + One(u8), + Two(u8), + Three +} +use Foo::*; + +fn main () { + let x = One(42); + if let One(n) | Two(n) = x { + println!("Got one or two with val: {}", n); + } +} +``` + +This RFC proposes to extend the notation for `if let` and `while let` to also cover multiple +patterns separated by pipes (`|`). + +# Detailed design + +Basically take the design proposed in the original RFC for [if-let](text/0160-if-let.md) and extend the notation to allow multiple patters + +# Drawbacks + +It's an additional feature in the language. + +# Alternatives + +Not doing anything and require multiple `if let` statements or an actual `match`. + +# Unresolved questions + +We could consider extending `if let` and `while let` to also include guards, but that has opens up the question about syntax (`if let Some(n) = x if n > 0` is not extremely pretty), so we should probably save that for an RFC of its own. From 6ffe96e8f89c66cd0d42441d01055f7fafd71bf0 Mon Sep 17 00:00:00 2001 From: Philip Munksgaard Date: Wed, 4 Mar 2015 18:20:42 +0100 Subject: [PATCH 3/5] Update 0000-if-while-let_multiple_patterns.md --- text/0000-if-while-let_multiple_patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-if-while-let_multiple_patterns.md b/text/0000-if-while-let_multiple_patterns.md index 574f702ec92..e034618cfb7 100644 --- a/text/0000-if-while-let_multiple_patterns.md +++ b/text/0000-if-while-let_multiple_patterns.md @@ -61,7 +61,7 @@ patterns separated by pipes (`|`). # Detailed design -Basically take the design proposed in the original RFC for [if-let](text/0160-if-let.md) and extend the notation to allow multiple patters +Basically take the design proposed in the original RFC for [if-let](text/0160-if-let.md) and extend the notation to allow multiple patterns # Drawbacks From 01df2c5a7bf8af2431570a6387386864d915b583 Mon Sep 17 00:00:00 2001 From: Philip Munksgaard Date: Wed, 4 Mar 2015 18:24:51 +0100 Subject: [PATCH 4/5] Update 0000-if-while-let_multiple_patterns.md --- text/0000-if-while-let_multiple_patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-if-while-let_multiple_patterns.md b/text/0000-if-while-let_multiple_patterns.md index e034618cfb7..d319f194bd1 100644 --- a/text/0000-if-while-let_multiple_patterns.md +++ b/text/0000-if-while-let_multiple_patterns.md @@ -61,7 +61,7 @@ patterns separated by pipes (`|`). # Detailed design -Basically take the design proposed in the original RFC for [if-let](text/0160-if-let.md) and extend the notation to allow multiple patterns +Basically take the design proposed in the original RFC for [if-let](0160-if-let.md) and extend the notation to allow multiple patterns # Drawbacks From 8cb952394151239067e752a71c8c26e6abea3533 Mon Sep 17 00:00:00 2001 From: Philip Munksgaard Date: Wed, 4 Mar 2015 18:27:03 +0100 Subject: [PATCH 5/5] Update 0000-if-while-let_multiple_patterns.md --- text/0000-if-while-let_multiple_patterns.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/text/0000-if-while-let_multiple_patterns.md b/text/0000-if-while-let_multiple_patterns.md index d319f194bd1..a58ca39d349 100644 --- a/text/0000-if-while-let_multiple_patterns.md +++ b/text/0000-if-while-let_multiple_patterns.md @@ -61,7 +61,15 @@ patterns separated by pipes (`|`). # Detailed design -Basically take the design proposed in the original RFC for [if-let](0160-if-let.md) and extend the notation to allow multiple patterns +Basically take the design proposed in the original RFC for [if-let](0160-if-let.md) and extend the notation to allow multiple patterns. + +Specifically, modify the grammar for `if-cond` to: + +``` +if-cond = 'let' pattern [ '|' pattern ] * '=' expression +``` + +and ditto for `while let`. # Drawbacks