Skip to content

Commit 2cd24ce

Browse files
committed
RFC: .. in patterns
1 parent ee281d7 commit 2cd24ce

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

text/0000-dotdot-in-patterns.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
- Feature Name: dotdot_in_patterns
2+
- Start Date: 2016-02-06
3+
- RFC PR: (leave this empty)
4+
- Rust Issue: (leave this empty)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Permit the `..` pattern fragment in more contexts.
10+
11+
# Motivation
12+
[motivation]: #motivation
13+
14+
The pattern fragment `..` can be used in some patterns to denote several elements in list contexts.
15+
However, it doesn't always compiles when used in such contexts.
16+
One can expect the ability to match tuple variants like `V(u8, u8, u8)` with patterns like
17+
`V(x, ..)` or `V(.., z)`, but the compiler rejects such patterns currently despite accepting
18+
very similar `V(..)`.
19+
20+
This RFC is intended to "complete" the feature and make it work in all possible list contexts,
21+
making the language a bit more convenient and consistent.
22+
23+
# Detailed design
24+
[design]: #detailed-design
25+
26+
Let's list all the patterns currently existing in the language, that contain lists of subpatterns:
27+
28+
```
29+
// Struct patterns.
30+
S { field1, field2, ..., fieldN }
31+
32+
// Tuple struct patterns.
33+
S(field1, field2, ..., fieldN)
34+
35+
// Tuple patterns.
36+
(field1, field2, ..., fieldN)
37+
38+
// Slice patterns.
39+
[elem1, elem2, ..., elemN]
40+
```
41+
In all the patterns above, except for struct patterns, field/element positions are significant.
42+
43+
Now list all the contexts that currently permit the `..` pattern fragment:
44+
```
45+
// Struct patterns, the last position.
46+
S { subpat1, subpat2, .. }
47+
48+
// Tuple struct patterns, the last and the only position, no extra subpatterns allowed.
49+
S(..)
50+
51+
// Slice patterns, the last position.
52+
[subpat1, subpat2, ..]
53+
// Slice patterns, the first position.
54+
[.., subpatN-1, subpatN]
55+
// Slice patterns, any other position.
56+
[subpat1, .., subpatN]
57+
// Slice patterns, any of the above with a subslice binding.
58+
// (The binding is not actually a binding, but one more pattern bound to the sublist, but this is
59+
// not important for our discussion.)
60+
[subpat1, binding.., subpatN]
61+
```
62+
Something is obviously missing, let's fill in the missing parts.
63+
64+
```
65+
// Struct patterns, the last position.
66+
S { subpat1, subpat2, .. }
67+
// **NOT PROPOSED**: Struct patterns, any position.
68+
// Since named struct fields are not positional, there's essentially no sense in placing the `..`
69+
// anywhere except for one conventionally chosen position (the last one) or in sublist bindings,
70+
// so we don't propose extensions to struct patterns.
71+
S { subpat1, .., subpatN }
72+
S { subpat1, binding.., subpatN }
73+
74+
// Tuple struct patterns, the last and the only position, no extra subpatterns allowed.
75+
S(..)
76+
// **NEW**: Tuple struct patterns, any position.
77+
S(subpat1, subpat2, ..)
78+
S(.., subpatN-1, subpatN)
79+
S(subpat1, .., subpatN)
80+
// **NEW**: Tuple struct patterns, any position with a sublist binding.
81+
// The binding has a tuple type.
82+
S(subpat1, binding.., subpatN)
83+
84+
// **NEW**: Tuple patterns, any position.
85+
(subpat1, subpat2, ..)
86+
(.., subpatN-1, subpatN)
87+
(subpat1, .., subpatN)
88+
// **NEW**: Tuple patterns, any position with a sublist binding.
89+
// The binding has a tuple type.
90+
(subpat1, binding.., subpatN)
91+
92+
// Slice patterns, the last position.
93+
[subpat1, subpat2, ..]
94+
// Slice patterns, the first position.
95+
[.., subpatN-1, subpatN]
96+
// Slice patterns, any other position.
97+
[subpat1, .., subpatN]
98+
// Slice patterns, any of the above with a subslice binding.
99+
[subpat1, binding.., subpatN]
100+
```
101+
102+
Trailing comma is not allowed after `..` in the last position by analogy with existing slice and
103+
struct patterns.
104+
105+
This RFC is not critically important and can be rolled out in parts, for example, bare `..` first,
106+
`..` with a sublist binding eventually.
107+
108+
# Drawbacks
109+
[drawbacks]: #drawbacks
110+
111+
None.
112+
113+
# Alternatives
114+
[alternatives]: #alternatives
115+
116+
None.
117+
118+
# Unresolved questions
119+
[unresolved]: #unresolved-questions
120+
121+
Sublist binding syntax conflicts with possible exclusive range patterns
122+
`begin .. end`/`begin..`/`..end`. This problem already exists for slice patterns and has to be
123+
solved independently from extensions to `..`.
124+
This RFC simply selects the same syntax that slice patterns already have.

0 commit comments

Comments
 (0)