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
Copy file name to clipboardExpand all lines: src/expressions/struct-expr.md
+48-24Lines changed: 48 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ StructExprField ->
21
21
| (IDENTIFIER | TUPLE_INDEX) `:` Expression
22
22
)
23
23
24
-
StructBase -> `..` Expression
24
+
StructBase -> `..` Expression?
25
25
26
26
StructExprTuple -> CallExpression
27
27
@@ -61,29 +61,6 @@ The field name is separated from its value with a colon.
61
61
r[expr.struct.field.union-constraint]
62
62
A value of a [union] type can only be created using this syntax, and it must specify exactly one field.
63
63
64
-
r[expr.struct.update]
65
-
## Functional update syntax
66
-
67
-
r[expr.struct.update.intro]
68
-
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.
69
-
70
-
r[expr.struct.update.base-same-type]
71
-
The expression following `..` (the base) must have the same struct type as the new struct type being formed.
72
-
73
-
r[expr.struct.update.fields]
74
-
The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.
75
-
76
-
r[expr.struct.update.visibility-constraint]
77
-
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
78
-
79
-
```rust
80
-
# structPoint3d { x:i32, y:i32, z:i32 }
81
-
letmutbase=Point3d {x:1, y:2, z:3};
82
-
lety_ref=&mutbase.y;
83
-
Point3d {y:0, z:10, ..base}; // OK, only base.x is accessed
84
-
drop(y_ref);
85
-
```
86
-
87
64
r[expr.struct.brace-restricted-positions]
88
65
Struct expressions with curly braces can't be used directly in a [loop] or [if] expression's head, or in the [scrutinee] of an [if let] or [match] expression.
89
66
However, struct expressions can be used in these situations if they are within another expression, for example inside [parentheses].
@@ -178,6 +155,52 @@ let d = ColorSpace::Oklch {};
Point3d {y:0, z:10, ..base}; // OK, only base.x is accessed
178
+
drop(y_ref);
179
+
```
180
+
181
+
r[expr.struct.default]
182
+
## Default field syntax
183
+
184
+
r[expr.struct.default.intro]
185
+
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].
186
+
187
+
r[expr.struct.default.fields]
188
+
All fields without defualt values must be listed in the expression.
189
+
The entire expression uses the given values for the fields that were specified and initializes the remaining fields with their respective default values.
190
+
191
+
r[expr.struct.default.visibility-constraint]
192
+
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
0 commit comments