@@ -2868,12 +2868,21 @@ tail value of `@Nil`. The second pattern matches _any_ list constructed with `Co
2868
2868
ignoring the values of its arguments. The difference between ` _ ` and ` * ` is that the pattern ` C(_) ` is only type-correct if
2869
2869
` C ` has exactly one argument, while the pattern ` C(..) ` is type-correct for any enum variant ` C ` , regardless of how many arguments ` C ` has.
2870
2870
2871
- To execute an ` match ` expression, first the head expression is evaluated, then
2872
- its value is sequentially compared to the patterns in the arms until a match
2871
+ A ` match ` behaves differently depending on whether or not the head expression
2872
+ is an [ lvalue or an rvalue] ( #lvalues-rvalues-and-temporaries ) .
2873
+ If the head expression is an rvalue, it is
2874
+ first evaluated into a temporary location, and the resulting value
2875
+ is sequentially compared to the patterns in the arms until a match
2873
2876
is found. The first arm with a matching pattern is chosen as the branch target
2874
2877
of the ` match ` , any variables bound by the pattern are assigned to local
2875
2878
variables in the arm's block, and control enters the block.
2876
2879
2880
+ When the head expression is an lvalue, the match does not allocate a
2881
+ temporary location (however, a by-value binding may copy or move from
2882
+ the lvalue). When possible, it is preferable to match on lvalues, as the
2883
+ lifetime of these matches inherits the lifetime of the lvalue, rather
2884
+ than being restricted to the inside of the match.
2885
+
2877
2886
An example of an ` match ` expression:
2878
2887
2879
2888
~~~~
@@ -2907,6 +2916,15 @@ This can be changed to bind to a borrowed pointer by
2907
2916
using the ``` ref ``` keyword,
2908
2917
or to a mutable borrowed pointer using ``` ref mut ``` .
2909
2918
2919
+ Patterns can also dereference pointers by using the `` & `` ,
2920
+ `` ~ `` or `` @ `` symbols, as appropriate. For example, these two matches
2921
+ on `` x: &int `` are equivalent:
2922
+
2923
+ ~~~~
2924
+ match *x { 0 => "zero", _ => "some" }
2925
+ match x { &0 => "zero", _ => "some" }
2926
+ ~~~~
2927
+
2910
2928
A pattern that's just an identifier,
2911
2929
like ` Nil ` in the previous answer,
2912
2930
could either refer to an enum variant that's in scope,
0 commit comments