From 042eb45a6601d4549b8a78d090eb84aec729a286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krasnoborski?= Date: Fri, 15 Aug 2014 22:40:51 -0700 Subject: [PATCH 1/2] RFC: Change syntax of subslice matching --- active/0000-subslice-syntax-change.md | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 active/0000-subslice-syntax-change.md diff --git a/active/0000-subslice-syntax-change.md b/active/0000-subslice-syntax-change.md new file mode 100644 index 00000000000..cc7ec48386e --- /dev/null +++ b/active/0000-subslice-syntax-change.md @@ -0,0 +1,55 @@ +- Start Date: 2014-08-15 +- RFC PR: +- Rust Issue: + +# Summary + +Change syntax of subslices matching from `..xs` to `xs..` +to be more consistent with the rest of the language +and allow future backwards compatible improvements. + +Small example: + +```rust +match slice { + [xs.., _] => xs, + [] => fail!() +} +``` + +This is basically heavily stripped version of [RFC 101](https://github.com/rust-lang/rfcs/pull/101). + +# Motivation + +In Rust, symbol after `..` token usually describes number of things, +as in `[T, ..N]` type or in `[e, ..N]` expression. +But in following pattern: `[_, ..xs]`, `xs` doesn't describe any number, +but the whole subslice. + +I propose to move dots to the right for several reasons (including one mentioned above): + +1. Looks more natural (but that might be subjective). +2. Consistent with the rest of the language. +3. C++ uses `args...` in variadic templates. +4. It allows extending slice pattern matching as described in [RFC 101](https://github.com/rust-lang/rfcs/pull/101). + +# Detailed design + +Slice matching grammar would change to (assuming trailing commas; +grammar syntax as in Rust manual): + + slice_pattern : "[" [[pattern | subslice_pattern] ","]* "]" ; + subslice_pattern : ["mut"? ident]? ".." ["@" slice_pattern]? ; + +# Drawbacks + +Backward incompatible. + +# Alternatives + +Don't do it at all. + +# Unresolved questions + +Whether subslice matching combined with `@` should be written as `xs.. @[1, 2]` +or maybe in another way: `xs @[1, 2]..`. From 62e3832a4c67be194c76405d5217c3ad1c000268 Mon Sep 17 00:00:00 2001 From: krdln Date: Thu, 28 Aug 2014 22:02:21 -0700 Subject: [PATCH 2/2] Added description of current grammar. --- active/0000-subslice-syntax-change.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/active/0000-subslice-syntax-change.md b/active/0000-subslice-syntax-change.md index cc7ec48386e..ff70630a1d9 100644 --- a/active/0000-subslice-syntax-change.md +++ b/active/0000-subslice-syntax-change.md @@ -41,6 +41,11 @@ grammar syntax as in Rust manual): slice_pattern : "[" [[pattern | subslice_pattern] ","]* "]" ; subslice_pattern : ["mut"? ident]? ".." ["@" slice_pattern]? ; +To compare, currently it looks like: + + slice_pattern : "[" [[pattern | subslice_pattern] ","]* "]" ; + subslice_pattern : ".." ["mut"? ident ["@" slice_pattern]?]? ; + # Drawbacks Backward incompatible.