@@ -2818,14 +2818,15 @@ match_pat : pat [ ".." pat ] ? [ "if" expr ] ;
2818
2818
2819
2819
A ` match ` expression branches on a * pattern* . The exact form of matching that
2820
2820
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.
2825
2826
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:
2829
2830
2830
2831
~~~~
2831
2832
enum List<X> { Nil, Cons(X, ~List<X>) }
@@ -2839,11 +2840,35 @@ match x {
2839
2840
}
2840
2841
~~~~
2841
2842
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
+ ~~~~
2847
2872
2848
2873
A ` match ` behaves differently depending on whether or not the head expression
2849
2874
is an [ lvalue or an rvalue] ( #lvalues-rvalues-and-temporaries ) .
@@ -2926,7 +2951,7 @@ let z = match x { &0 => "zero", _ => "some" };
2926
2951
assert_eq!(y, z);
2927
2952
~~~~
2928
2953
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 ,
2930
2955
could either refer to an enum variant that's in scope, or bind a new variable.
2931
2956
The compiler resolves this ambiguity by forbidding variable bindings that occur
2932
2957
in ` match ` patterns from shadowing names of variants that are in scope.
0 commit comments