Skip to content

Commit dfb6116

Browse files
committed
auto merge of #11877 : Armavica/rust/doc-pm-vector, r=alexcrichton
This feature already was in the tutorial, but I believe that the tutorial should be a strict subset of the manual. I also added an example.
2 parents c3ae182 + d62097d commit dfb6116

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
@@ -2818,14 +2818,15 @@ match_pat : pat [ ".." pat ] ? [ "if" expr ] ;
28182818

28192819
A `match` expression branches on a *pattern*. The exact form of matching that
28202820
occurs depends on the pattern. Patterns consist of some combination of
2821-
literals, destructured enum constructors, structures, records and tuples, variable binding
2822-
specifications, wildcards (`..`), and placeholders (`_`). A `match` expression has a *head
2823-
expression*, which is the value to compare to the patterns. The type of the
2824-
patterns must equal the type of the head expression.
2821+
literals, destructured vectors or enum constructors, structures, records and
2822+
tuples, variable binding specifications, wildcards (`..`), and placeholders
2823+
(`_`). A `match` expression has a *head expression*, which is the value to
2824+
compare to the patterns. The type of the patterns must equal the type of the
2825+
head expression.
28252826

2826-
In a pattern whose head expression has an `enum` type, a placeholder (`_`) stands for a
2827-
*single* data field, whereas a wildcard `..` stands for *all* the fields of a particular
2828-
variant. For example:
2827+
In a pattern whose head expression has an `enum` type, a placeholder (`_`)
2828+
stands for a *single* data field, whereas a wildcard `..` stands for *all* the
2829+
fields of a particular variant. For example:
28292830

28302831
~~~~
28312832
enum List<X> { Nil, Cons(X, ~List<X>) }
@@ -2839,11 +2840,35 @@ match x {
28392840
}
28402841
~~~~
28412842

2842-
The first pattern matches lists constructed by applying `Cons` to any head value, and a
2843-
tail value of `~Nil`. The second pattern matches _any_ list constructed with `Cons`,
2844-
ignoring the values of its arguments. The difference between `_` and `..` is that the pattern
2845-
`C(_)` is only type-correct if `C` has exactly one argument, while the pattern `C(..)` is
2846-
type-correct for any enum variant `C`, regardless of how many arguments `C` has.
2843+
The first pattern matches lists constructed by applying `Cons` to any head
2844+
value, and a tail value of `~Nil`. The second pattern matches _any_ list
2845+
constructed with `Cons`, ignoring the values of its arguments. The difference
2846+
between `_` and `..` is that the pattern `C(_)` is only type-correct if `C` has
2847+
exactly one argument, while the pattern `C(..)` is type-correct for any enum
2848+
variant `C`, regardless of how many arguments `C` has.
2849+
2850+
Used inside a vector pattern, `..` stands for any number of elements. This
2851+
wildcard can be used at most once for a given vector, which implies that it
2852+
cannot be used to specifically match elements that are at an unknown distance
2853+
from both ends of a vector, like `[.., 42, ..]`. If followed by a variable name,
2854+
it will bind the corresponding slice to the variable. Example:
2855+
2856+
~~~~
2857+
fn is_symmetric(list: &[uint]) -> bool {
2858+
match list {
2859+
[] | [_] => true,
2860+
[x, ..inside, y] if x == y => is_symmetric(inside),
2861+
_ => false
2862+
}
2863+
}
2864+
2865+
fn main() {
2866+
let sym = &[0, 1, 4, 2, 4, 1, 0];
2867+
let not_sym = &[0, 1, 7, 2, 4, 1, 0];
2868+
assert!(is_symmetric(sym));
2869+
assert!(!is_symmetric(not_sym));
2870+
}
2871+
~~~~
28472872

28482873
A `match` behaves differently depending on whether or not the head expression
28492874
is an [lvalue or an rvalue](#lvalues-rvalues-and-temporaries).
@@ -2926,7 +2951,7 @@ let z = match x { &0 => "zero", _ => "some" };
29262951
assert_eq!(y, z);
29272952
~~~~
29282953

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

0 commit comments

Comments
 (0)