@@ -2864,14 +2864,15 @@ match_pat : pat [ ".." pat ] ? [ "if" expr ] ;
2864
2864
2865
2865
A ` match ` expression branches on a * pattern* . The exact form of matching that
2866
2866
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.
2871
2872
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:
2875
2876
2876
2877
~~~~
2877
2878
enum List<X> { Nil, Cons(X, ~List<X>) }
@@ -2885,11 +2886,35 @@ match x {
2885
2886
}
2886
2887
~~~~
2887
2888
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
+ ~~~~
2893
2918
2894
2919
A ` match ` behaves differently depending on whether or not the head expression
2895
2920
is an [ lvalue or an rvalue] ( #lvalues-rvalues-and-temporaries ) .
@@ -2972,7 +2997,7 @@ let z = match x { &0 => "zero", _ => "some" };
2972
2997
assert_eq!(y, z);
2973
2998
~~~~
2974
2999
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 ,
2976
3001
could either refer to an enum variant that's in scope, or bind a new variable.
2977
3002
The compiler resolves this ambiguity by forbidding variable bindings that occur
2978
3003
in ` match ` patterns from shadowing names of variants that are in scope.
0 commit comments