Skip to content

Commit d62097d

Browse files
committed
Document vector destructuring with wildcard '..'
1 parent 2b93925 commit d62097d

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

doc/rust.md

+38-13
Original file line numberDiff line numberDiff line change
@@ -2864,14 +2864,15 @@ match_pat : pat [ ".." pat ] ? [ "if" expr ] ;
28642864

28652865
A `match` expression branches on a *pattern*. The exact form of matching that
28662866
occurs depends on the pattern. Patterns consist of some combination of
2867-
literals, destructured enum constructors, structures, records and tuples, variable binding
2868-
specifications, wildcards (`..`), and placeholders (`_`). A `match` expression has a *head
2869-
expression*, which is the value to compare to the patterns. The type of the
2870-
patterns must equal the type of the head expression.
2867+
literals, destructured vectors or enum constructors, structures, records and
2868+
tuples, variable binding specifications, wildcards (`..`), and placeholders
2869+
(`_`). A `match` expression has a *head expression*, which is the value to
2870+
compare to the patterns. The type of the patterns must equal the type of the
2871+
head expression.
28712872

2872-
In a pattern whose head expression has an `enum` type, a placeholder (`_`) stands for a
2873-
*single* data field, whereas a wildcard `..` stands for *all* the fields of a particular
2874-
variant. For example:
2873+
In a pattern whose head expression has an `enum` type, a placeholder (`_`)
2874+
stands for a *single* data field, whereas a wildcard `..` stands for *all* the
2875+
fields of a particular variant. For example:
28752876

28762877
~~~~
28772878
enum List<X> { Nil, Cons(X, ~List<X>) }
@@ -2885,11 +2886,35 @@ match x {
28852886
}
28862887
~~~~
28872888

2888-
The first pattern matches lists constructed by applying `Cons` to any head value, and a
2889-
tail value of `~Nil`. The second pattern matches _any_ list constructed with `Cons`,
2890-
ignoring the values of its arguments. The difference between `_` and `..` is that the pattern
2891-
`C(_)` is only type-correct if `C` has exactly one argument, while the pattern `C(..)` is
2892-
type-correct for any enum variant `C`, regardless of how many arguments `C` has.
2889+
The first pattern matches lists constructed by applying `Cons` to any head
2890+
value, and a tail value of `~Nil`. The second pattern matches _any_ list
2891+
constructed with `Cons`, ignoring the values of its arguments. The difference
2892+
between `_` and `..` is that the pattern `C(_)` is only type-correct if `C` has
2893+
exactly one argument, while the pattern `C(..)` is type-correct for any enum
2894+
variant `C`, regardless of how many arguments `C` has.
2895+
2896+
Used inside a vector pattern, `..` stands for any number of elements. This
2897+
wildcard can be used at most once for a given vector, which implies that it
2898+
cannot be used to specifically match elements that are at an unknown distance
2899+
from both ends of a vector, like `[.., 42, ..]`. If followed by a variable name,
2900+
it will bind the corresponding slice to the variable. Example:
2901+
2902+
~~~~
2903+
fn is_symmetric(list: &[uint]) -> bool {
2904+
match list {
2905+
[] | [_] => true,
2906+
[x, ..inside, y] if x == y => is_symmetric(inside),
2907+
_ => false
2908+
}
2909+
}
2910+
2911+
fn main() {
2912+
let sym = &[0, 1, 4, 2, 4, 1, 0];
2913+
let not_sym = &[0, 1, 7, 2, 4, 1, 0];
2914+
assert!(is_symmetric(sym));
2915+
assert!(!is_symmetric(not_sym));
2916+
}
2917+
~~~~
28932918

28942919
A `match` behaves differently depending on whether or not the head expression
28952920
is an [lvalue or an rvalue](#lvalues-rvalues-and-temporaries).
@@ -2972,7 +2997,7 @@ let z = match x { &0 => "zero", _ => "some" };
29722997
assert_eq!(y, z);
29732998
~~~~
29742999

2975-
A pattern that's just an identifier, like `Nil` in the previous answer,
3000+
A pattern that's just an identifier, like `Nil` in the previous example,
29763001
could either refer to an enum variant that's in scope, or bind a new variable.
29773002
The compiler resolves this ambiguity by forbidding variable bindings that occur
29783003
in `match` patterns from shadowing names of variants that are in scope.

0 commit comments

Comments
 (0)