@@ -123,13 +123,15 @@ comma:
123
123
124
124
## Struct expressions
125
125
126
- There are several forms of struct expressions. A _ struct expression_
127
- consists of the [ path] ( paths.html ) of a [ struct item] ( items.html#structs ) , followed
128
- by a brace-enclosed list of zero or more comma-separated name-value pairs,
129
- providing the field values of a new instance of the struct. A field name can be
130
- any identifier, and is separated from its value expression by a colon. The
131
- location denoted by a struct field is mutable if and only if the enclosing
132
- struct is mutable.
126
+ There are several forms of struct expressions. A _ struct expression_ consists
127
+ of the [ path] ( paths.html ) of a [ struct item] ( items.html#structs ) , followed by a
128
+ brace-enclosed list of zero or more comma-separated name-value pairs, providing
129
+ the field values of a new instance of the struct. A field name can be any
130
+ identifier, and is separated from its value expression by a colon. In the case
131
+ of a tuple struct the field names are numbers corresponding to the position of
132
+ the field. The numbers must be written in decimal, containing no underscores
133
+ and with no leading zeros or integer suffix. The location denoted by a struct
134
+ field is mutable if and only if the enclosing struct is mutable.
133
135
134
136
A _ tuple struct expression_ consists of the [ path] ( paths.html ) of a [ struct
135
137
item] ( items.html#structs ) , followed by a parenthesized list of one or more
@@ -150,6 +152,7 @@ The following are examples of struct expressions:
150
152
Point {x : 10.0 , y : 20.0 };
151
153
NothingInMe {};
152
154
TuplePoint (10.0 , 20.0 );
155
+ TuplePoint { 0 : 10.0 , 1 : 20.0 }; // Results in the same value as the above line
153
156
let u = game :: User {name : " Joe" , age : 35 , score : 100_000 };
154
157
some_fn :: <Cookie >(Cookie );
155
158
```
@@ -174,9 +177,9 @@ Point3d {y: 0, z: 10, .. base};
174
177
175
178
#### Struct field init shorthand
176
179
177
- When initializing a data structure (struct, enum, union) with named fields,
178
- it is allowed to write ` fieldname ` as a shorthand for ` fieldname: fieldname ` .
179
- This allows a compact syntax with less duplication.
180
+ When initializing a data structure (struct, enum, union) with named (but not
181
+ numbered) fields, it is allowed to write ` fieldname ` as a shorthand for
182
+ ` fieldname: fieldname ` . This allows a compact syntax with less duplication.
180
183
181
184
Example:
182
185
@@ -777,10 +780,6 @@ variable binding specifications, wildcards (`..`), and placeholders (`_`). A
777
780
the patterns. The type of the patterns must equal the type of the head
778
781
expression.
779
782
780
- In a pattern whose head expression has an ` enum ` type, a placeholder (` _ ` )
781
- stands for a * single* data field, whereas a wildcard ` .. ` stands for * all* the
782
- fields of a particular variant.
783
-
784
783
A ` match ` behaves differently depending on whether or not the head expression
785
784
is an [ lvalue or an rvalue] ( expressions.html#lvalues-rvalues-and-temporaries ) .
786
785
If the head expression is an rvalue, it is first evaluated into a temporary
@@ -816,16 +815,31 @@ matched value (depending on the matched value's type). This can be changed to
816
815
bind to a reference by using the ` ref ` keyword, or to a mutable reference using
817
816
` ref mut ` .
818
817
819
- Subpatterns can also be bound to variables by the use of the syntax `variable @
820
- subpattern`. For example:
818
+ Patterns can be used to * destructure* structs, enums, and tuples. Destructuring
819
+ breaks a value up into its component pieces. The syntax used is the same as
820
+ when creating such values. When destructing a data structure with named (but
821
+ not numbered) fields, it is allowed to write ` fieldname ` as a shorthand for
822
+ ` fieldname: fieldname ` . In a pattern whose head expression has a ` struct ` ,
823
+ ` enum ` or ` tupl ` type, a placeholder (` _ ` ) stands for a * single* data field,
824
+ whereas a wildcard ` .. ` stands for * all* the fields of a particular variant.
821
825
822
826
``` rust
823
- let x = 1 ;
824
-
825
- match x {
826
- e @ 1 ... 5 => println! (" got a range element {}" , e ),
827
- _ => println! (" anything" ),
828
- }
827
+ # enum Message {
828
+ # Quit ,
829
+ # WriteString (String ),
830
+ # Move { x : i32 , y : i32 },
831
+ # ChangeColor (u8 , u8 , u8 ),
832
+ # }
833
+ # let message = Message :: Quit ;
834
+ match message {
835
+ Message :: Quit => println! (" Quit" ),
836
+ Message :: WriteString (write ) => println! (" {}" , & write ),
837
+ Message :: Move { x , y : 0 } => println! (" move {} horizontally" , x ),
838
+ Message :: Move { .. } => println! (" other move" ),
839
+ Message :: ChangeColor { 0 : red , 1 : green , 2 : _ } => {
840
+ println! (" color change, red: {}, green: {}" , red , green );
841
+ }
842
+ };
829
843
```
830
844
831
845
Patterns can also dereference pointers by using the ` & ` , ` &mut ` and ` box `
@@ -840,6 +854,18 @@ let z = match x { &0 => "zero", _ => "some" };
840
854
assert_eq! (y , z );
841
855
```
842
856
857
+ Subpatterns can also be bound to variables by the use of the syntax `variable @
858
+ subpattern`. For example:
859
+
860
+ ``` rust
861
+ let x = 1 ;
862
+
863
+ match x {
864
+ e @ 1 ... 5 => println! (" got a range element {}" , e ),
865
+ _ => println! (" anything" ),
866
+ }
867
+ ```
868
+
843
869
Multiple match patterns may be joined with the ` | ` operator. A range of values
844
870
may be specified with ` ... ` . For example:
845
871
0 commit comments