Skip to content

Commit cf5dc8c

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

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

text/0000-dotdot-in-patterns.md

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

0 commit comments

Comments
 (0)