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-24
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 ->
27
27
PathInExpression `(`
@@ -62,29 +62,6 @@ The field name is separated from its value with a colon.
62
62
r[expr.struct.field.union-constraint]
63
63
A value of a [union] type can only be created using this syntax, and it must specify exactly one field.
64
64
65
-
r[expr.struct.update]
66
-
## Functional update syntax
67
-
68
-
r[expr.struct.update.intro]
69
-
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.
70
-
71
-
r[expr.struct.update.base-same-type]
72
-
The expression following `..` (the base) must have the same struct type as the new struct type being formed.
73
-
74
-
r[expr.struct.update.fields]
75
-
The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.
76
-
77
-
r[expr.struct.update.visibility-constraint]
78
-
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
79
-
80
-
```rust
81
-
# structPoint3d { x:i32, y:i32, z:i32 }
82
-
letmutbase=Point3d {x:1, y:2, z:3};
83
-
lety_ref=&mutbase.y;
84
-
Point3d {y:0, z:10, ..base}; // OK, only base.x is accessed
85
-
drop(y_ref);
86
-
```
87
-
88
65
r[expr.struct.brace-restricted-positions]
89
66
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.
90
67
However, struct expressions can be used in these situations if they are within another expression, for example inside [parentheses].
@@ -142,6 +119,52 @@ let a = Gamma; // Gamma unit value.
142
119
letb=Gamma{}; // Exact same value as `a`.
143
120
```
144
121
122
+
r[expr.struct.update]
123
+
## Functional update syntax
124
+
125
+
r[expr.struct.update.intro]
126
+
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.
127
+
128
+
r[expr.struct.update.base-same-type]
129
+
The expression following `..` (the base) must have the same struct type as the new struct type being formed.
130
+
131
+
r[expr.struct.update.fields]
132
+
The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.
133
+
134
+
r[expr.struct.update.visibility-constraint]
135
+
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
136
+
137
+
```rust
138
+
# structPoint3d { x:i32, y:i32, z:i32 }
139
+
letmutbase=Point3d {x:1, y:2, z:3};
140
+
lety_ref=&mutbase.y;
141
+
Point3d {y:0, z:10, ..base}; // OK, only base.x is accessed
142
+
drop(y_ref);
143
+
```
144
+
145
+
r[expr.struct.default]
146
+
## Default field syntax
147
+
148
+
r[expr.struct.default.intro]
149
+
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].
150
+
151
+
r[expr.struct.default.fields]
152
+
All fields without defualt values must be listed in the expression.
153
+
The entire expression uses the given values for the fields that were specified and initializes the remaining fields with their respective default values.
154
+
155
+
r[expr.struct.default.visibility-constraint]
156
+
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
157
+
158
+
```rust
159
+
structPet {
160
+
name:Option<String>,
161
+
age:i128=42,
162
+
}
163
+
164
+
letpet=Pet { name:None, .. };
165
+
assert_eq!(valid.age, 42);
166
+
```
167
+
145
168
[call expression]: call-expr.md
146
169
[enum variant]: ../items/enumerations.md
147
170
[if let]: if-expr.md#if-let-expressions
@@ -153,3 +176,4 @@ let b = Gamma{}; // Exact same value as `a`.
0 commit comments