@@ -123,13 +123,15 @@ comma:
123123
124124## Struct expressions
125125
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.
133135
134136A _ tuple struct expression_ consists of the [ path] ( paths.html ) of a [ struct
135137item] ( items.html#structs ) , followed by a parenthesized list of one or more
@@ -150,6 +152,7 @@ The following are examples of struct expressions:
150152Point {x : 10.0 , y : 20.0 };
151153NothingInMe {};
152154TuplePoint (10.0 , 20.0 );
155+ TuplePoint { 0 : 10.0 , 1 : 20.0 }; // Results in the same value as the above line
153156let u = game :: User {name : " Joe" , age : 35 , score : 100_000 };
154157some_fn :: <Cookie >(Cookie );
155158```
@@ -174,9 +177,9 @@ Point3d {y: 0, z: 10, .. base};
174177
175178#### Struct field init shorthand
176179
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.
180183
181184Example:
182185
@@ -698,10 +701,6 @@ variable binding specifications, wildcards (`..`), and placeholders (`_`). A
698701the patterns. The type of the patterns must equal the type of the head
699702expression.
700703
701- In a pattern whose head expression has an ` enum ` type, a placeholder (` _ ` )
702- stands for a * single* data field, whereas a wildcard ` .. ` stands for * all* the
703- fields of a particular variant.
704-
705704A ` match ` behaves differently depending on whether or not the head expression
706705is an [ lvalue or an rvalue] ( expressions.html#lvalues-rvalues-and-temporaries ) .
707706If the head expression is an rvalue, it is first evaluated into a temporary
@@ -737,16 +736,31 @@ matched value (depending on the matched value's type). This can be changed to
737736bind to a reference by using the ` ref ` keyword, or to a mutable reference using
738737` ref mut ` .
739738
740- Subpatterns can also be bound to variables by the use of the syntax `variable @
741- subpattern`. For example:
739+ Patterns can be used to * destructure* structs, enums, and tuples. Destructuring
740+ breaks a value up into its component pieces. The syntax used is the same as
741+ when creating such values. When destructing a data structure with named (but
742+ not numbered) fields, it is allowed to write ` fieldname ` as a shorthand for
743+ ` fieldname: fieldname ` . In a pattern whose head expression has a ` struct ` ,
744+ ` enum ` or ` tupl ` type, a placeholder (` _ ` ) stands for a * single* data field,
745+ whereas a wildcard ` .. ` stands for * all* the fields of a particular variant.
742746
743747``` rust
744- let x = 1 ;
745-
746- match x {
747- e @ 1 ... 5 => println! (" got a range element {}" , e ),
748- _ => println! (" anything" ),
749- }
748+ # enum Message {
749+ # Quit ,
750+ # WriteString (String ),
751+ # Move { x : i32 , y : i32 },
752+ # ChangeColor (u8 , u8 , u8 ),
753+ # }
754+ # let message = Message :: Quit ;
755+ match message {
756+ Message :: Quit => println! (" Quit" ),
757+ Message :: WriteString (write ) => println! (" {}" , & write ),
758+ Message :: Move { x , y : 0 } => println! (" move {} horizontally" , x ),
759+ Message :: Move { .. } => println! (" other move" ),
760+ Message :: ChangeColor { 0 : red , 1 : green , 2 : _ } => {
761+ println! (" color change, red: {}, green: {}" , red , green );
762+ }
763+ };
750764```
751765
752766Patterns can also dereference pointers by using the ` & ` , ` &mut ` and ` box `
@@ -761,6 +775,18 @@ let z = match x { &0 => "zero", _ => "some" };
761775assert_eq! (y , z );
762776```
763777
778+ Subpatterns can also be bound to variables by the use of the syntax `variable @
779+ subpattern`. For example:
780+
781+ ``` rust
782+ let x = 1 ;
783+
784+ match x {
785+ e @ 1 ... 5 => println! (" got a range element {}" , e ),
786+ _ => println! (" anything" ),
787+ }
788+ ```
789+
764790Multiple match patterns may be joined with the ` | ` operator. A range of values
765791may be specified with ` ... ` . For example:
766792
0 commit comments