-
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: Change syntax of subslice matching #202
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
- 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]? ; | ||
|
||
To compare, currently it looks like: | ||
|
||
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]` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you clarify how it is written at present and with this proposal? |
||
or maybe in another way: `xs @[1, 2]..`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What in particular is it consistent with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I shouldn't say that it is consistent, but rather that it is not inconsistent. Current syntax suggest that when matching with
..x
,x
would be bound to a count of elements, but it is somewhat surprisingly bound to whole slice. In my previous RFC I proposed thatx
in this case should really mean a number, and that would require movingxs
to the left of..
. This proposal alone makes less sense, because it somewhat cut down version, but still I think proposed behaviour is less surprising than current and provides ability to make future backwards compatible changes (which would allow consistent behaviour of token after..
meaning a number).Unfortunatelly, as @glaebhoerl noticed, I didn't think about syntax of struct update at all.