You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Point3d {y:0, z:10, ..base}; // OK, only base.x is accessed
114
-
drop(y_ref);
115
-
```
116
-
117
97
r[expr.struct.brace-restricted-positions]
118
98
Structexpressionscan'tbeuseddirectlyina [loop] or [if] expression'shead, orinthe [scrutinee] ofan [iflet] or [match] expression.
119
99
However, structexpressionscanbeusedinthesesituationsiftheyarewithinanotherexpression, forexampleinside [parentheses].
@@ -130,6 +110,7 @@ let c3 = Color{1: 0, ..c2}; // Fill out all other fields using a base struct.
130
110
```
131
111
132
112
r[expr.struct.field.named]
113
+
133
114
### Struct field init shorthand
134
115
135
116
When initializing a data structure (struct, enum, union) with named (but not numbered) fields, it is allowed to write `fieldname` as a shorthand for `fieldname: fieldname`.
A struct expression that constructs a value of a struct type can terminate with the syntax `..` followed by an expression to denote a functional update.
135
+
136
+
r[expr.struct.update.base-same-type]
137
+
The expression following `..` (the base) must have the same struct type as the new struct type being formed.
138
+
139
+
r[expr.struct.update.fields]
140
+
The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.
141
+
142
+
r[expr.struct.update.visibility-constraint]
143
+
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
144
+
145
+
```rust
146
+
# structPoint3d { x:i32, y:i32, z:i32 }
147
+
letmutbase=Point3d {x:1, y:2, z:3};
148
+
lety_ref=&mutbase.y;
149
+
Point3d {y:0, z:10, ..base}; // OK, only base.x is accessed
150
+
drop(y_ref);
151
+
```
152
+
153
+
r[expr.struct.default]
154
+
155
+
## Default field syntax
156
+
157
+
r[expr.struct.default.intro]
158
+
A struct expression that constructs a value of a struct type can terminate with the syntax `..` without a following expression to denote that unlisted fields should be set to their [default values].
159
+
160
+
r[expr.struct.default.fields]
161
+
All fields without defualt values must be listed in the expression.
162
+
The entire expression uses the given values for the fields that were specified and initializes the remaining fields with their respective default values.
163
+
164
+
r[expr.struct.default.visibility-constraint]
165
+
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
0 commit comments