]>
Commit | Line | Data |
---|---|---|
ea8adc8c XL |
1 | # Struct expressions |
2 | ||
0bf4aa26 XL |
3 | > **<sup>Syntax</sup>**\ |
4 | > _StructExpression_ :\ | |
5 | > _StructExprStruct_\ | |
6 | > | _StructExprTuple_\ | |
7 | > | _StructExprUnit_ | |
8 | > | |
9 | > _StructExprStruct_ :\ | |
13cf67c4 | 10 | > [_PathInExpression_] `{` [_InnerAttribute_]<sup>\*</sup> (_StructExprFields_ | _StructBase_)<sup>?</sup> `}` |
0bf4aa26 XL |
11 | > |
12 | > _StructExprFields_ :\ | |
13 | > _StructExprField_ (`,` _StructExprField_)<sup>\*</sup> (`,` _StructBase_ | `,`<sup>?</sup>) | |
14 | > | |
15 | > _StructExprField_ :\ | |
16 | > [IDENTIFIER]\ | |
17 | > | ([IDENTIFIER] | [TUPLE_INDEX]) `:` [_Expression_] | |
18 | > | |
13cf67c4 XL |
19 | > _StructBase_ :\ |
20 | > `..` [_Expression_] | |
0bf4aa26 XL |
21 | > |
22 | > _StructExprTuple_ :\ | |
23 | > [_PathInExpression_] `(`\ | |
13cf67c4 | 24 | > [_InnerAttribute_]<sup>\*</sup>\ |
0bf4aa26 XL |
25 | > ( [_Expression_] (`,` [_Expression_])<sup>\*</sup> `,`<sup>?</sup> )<sup>?</sup>\ |
26 | > `)` | |
27 | > | |
28 | > _StructExprUnit_ : [_PathInExpression_] | |
29 | ||
30 | A _struct expression_ creates a struct or union value. It consists of a path to a [struct] | |
31 | or [union] item followed by the values for the fields of the item. There are three forms | |
32 | of struct expressions: struct, tuple, and unit. | |
ea8adc8c XL |
33 | |
34 | The following are examples of struct expressions: | |
35 | ||
36 | ```rust | |
37 | # struct Point { x: f64, y: f64 } | |
38 | # struct NothingInMe { } | |
39 | # struct TuplePoint(f64, f64); | |
40 | # mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } } | |
41 | # struct Cookie; fn some_fn<T>(t: T) {} | |
42 | Point {x: 10.0, y: 20.0}; | |
43 | NothingInMe {}; | |
44 | TuplePoint(10.0, 20.0); | |
45 | TuplePoint { 0: 10.0, 1: 20.0 }; // Results in the same value as the above line | |
46 | let u = game::User {name: "Joe", age: 35, score: 100_000}; | |
47 | some_fn::<Cookie>(Cookie); | |
48 | ``` | |
49 | ||
0bf4aa26 XL |
50 | ## Field struct expression |
51 | ||
52 | A struct expression with fields enclosed in curly braces allows you to specify the value | |
53 | for each individual field in any order. The field name is separated from its value with a | |
54 | colon. | |
55 | ||
56 | A value of a [union] type can also be created using this syntax, except that it must | |
57 | specify exactly one field. | |
ea8adc8c | 58 | |
532ac7d7 XL |
59 | ## Functional update syntax |
60 | ||
ea8adc8c XL |
61 | A struct expression can terminate with the syntax `..` followed by an |
62 | expression to denote a functional update. The expression following `..` (the | |
532ac7d7 XL |
63 | base) must have the same struct type as the new struct type being formed. |
64 | ||
65 | The entire expression uses the given values for the fields that were specified | |
66 | and moves or copies the remaining fields from the base expression. As with all | |
67 | struct expressions, all of the fields of the struct must be [visible], even | |
68 | those not explicitly named. | |
ea8adc8c XL |
69 | |
70 | ```rust | |
71 | # struct Point3d { x: i32, y: i32, z: i32 } | |
532ac7d7 XL |
72 | let mut base = Point3d {x: 1, y: 2, z: 3}; |
73 | let y_ref = &mut base.y; | |
74 | Point3d {y: 0, z: 10, .. base}; // OK, only base.x is accessed | |
75 | drop(y_ref); | |
ea8adc8c XL |
76 | ``` |
77 | ||
9fa01778 XL |
78 | Struct expressions with curly braces can't be used directly in a [loop] or [if] |
79 | expression's head, or in the [scrutinee] of an [if let] or [match] expression. | |
80 | However, struct expressions can be in used in these situations if they are | |
81 | within another expression, for example inside [parentheses]. | |
0bf4aa26 XL |
82 | |
83 | The field names can be decimal integer values to specify indices for constructing tuple | |
84 | structs. This can be used with base structs to fill out the remaining indices not | |
85 | specified: | |
86 | ||
87 | ```rust | |
88 | struct Color(u8, u8, u8); | |
89 | let c1 = Color(0, 0, 0); // Typical way of creating a tuple struct. | |
90 | let c2 = Color{0: 255, 1: 127, 2: 0}; // Specifying fields by index. | |
91 | let c3 = Color{1: 0, ..c2}; // Fill out all other fields using a base struct. | |
92 | ``` | |
93 | ||
94 | ### Struct field init shorthand | |
ea8adc8c XL |
95 | |
96 | When initializing a data structure (struct, enum, union) with named (but not | |
97 | numbered) fields, it is allowed to write `fieldname` as a shorthand for | |
98 | `fieldname: fieldname`. This allows a compact syntax with less duplication. | |
0bf4aa26 | 99 | For example: |
ea8adc8c XL |
100 | |
101 | ```rust | |
102 | # struct Point3d { x: i32, y: i32, z: i32 } | |
103 | # let x = 0; | |
104 | # let y_value = 0; | |
105 | # let z = 0; | |
106 | Point3d { x: x, y: y_value, z: z }; | |
107 | Point3d { x, y: y_value, z }; | |
108 | ``` | |
ff7c6d11 | 109 | |
0bf4aa26 XL |
110 | ## Tuple struct expression |
111 | ||
112 | A struct expression with fields enclosed in parentheses constructs a tuple struct. Though | |
113 | it is listed here as a specific expression for completeness, it is equivalent to a [call | |
114 | expression] to the tuple struct's constructor. For example: | |
115 | ||
116 | ```rust | |
117 | struct Position(i32, i32, i32); | |
118 | Position(0, 0, 0); // Typical way of creating a tuple struct. | |
119 | let c = Position; // `c` is a function that takes 3 arguments. | |
120 | let pos = c(8, 6, 7); // Creates a `Position` value. | |
121 | ``` | |
122 | ||
123 | ## Unit struct expression | |
124 | ||
125 | A unit struct expression is just the path to a unit struct item. This refers to the unit | |
126 | struct's implicit constant of its value. The unit struct value can also be constructed | |
127 | with a fieldless struct expression. For example: | |
128 | ||
129 | ```rust | |
130 | struct Gamma; | |
131 | let a = Gamma; // Gamma unit value. | |
132 | let b = Gamma{}; // Exact same value as `a`. | |
133 | ``` | |
134 | ||
13cf67c4 XL |
135 | ## Struct expression attributes |
136 | ||
137 | [Inner attributes] are allowed directly after the opening brace or parenthesis | |
138 | of a struct expression in the same expression contexts as [attributes on block | |
139 | expressions]. | |
0bf4aa26 | 140 | |
ff7c6d11 | 141 | [IDENTIFIER]: identifiers.html |
13cf67c4 | 142 | [Inner attributes]: attributes.html |
0bf4aa26 XL |
143 | [TUPLE_INDEX]: tokens.html#integer-literals |
144 | [_Expression_]: expressions.html | |
13cf67c4 | 145 | [_InnerAttribute_]: attributes.html |
0bf4aa26 | 146 | [_PathInExpression_]: paths.html#paths-in-expressions |
13cf67c4 | 147 | [attributes on block expressions]: expressions/block-expr.html#attributes-on-block-expressions |
0bf4aa26 | 148 | [call expression]: expressions/call-expr.html |
ff7c6d11 | 149 | [if let]: expressions/if-expr.html#if-let-expressions |
0bf4aa26 XL |
150 | [if]: expressions/if-expr.html#if-expressions |
151 | [loop]: expressions/loop-expr.html | |
8faf50e0 | 152 | [match]: expressions/match-expr.html |
0bf4aa26 | 153 | [parentheses]: http://localhost:3000/expressions/grouped-expr.html |
0bf4aa26 XL |
154 | [struct]: items/structs.html |
155 | [union]: items/unions.html | |
156 | [visible]: visibility-and-privacy.html | |
9fa01778 | 157 | [scrutinee]: glossary.html#scrutinee |