Skip to content

Commit 6e1eef6

Browse files
committed
Clarify what access struct updates do
1 parent db4f1f2 commit 6e1eef6

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/expressions.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,16 @@ expressions are value expressions.
9898

9999
A *value expression* is an expression that represents an actual value.
100100

101-
The left operand of an [assignment][assign] or [compound assignment] expression
102-
is a place expression context, as is the single operand of a unary [borrow], and
103-
the operand of any [implicit borrow]. The discriminant or subject of a
104-
[match expression][match] and right side of a [let statement] is also a place
105-
expression context. All other expression contexts are value expression contexts.
101+
The following contexts are *place expression* contexts:
102+
103+
* The left operand of an [assignment][assign] or [compound assignment] expression.
104+
* The operand of a unary [borrow] or [dereference] operator.
105+
* The operand of a field expression.
106+
* The indexed operand of an array indexing expression.
107+
* The operand of any [implicit borrow].
108+
* The initializer of a [let statement].
109+
* The [scrutinee] of a [match expression][match].
110+
* The base of a [functional update] struct expression.
106111

107112
> Note: Historically, place expressions were called *lvalues* and value
108113
> expressions were called *rvalues*.
@@ -279,6 +284,7 @@ They are never allowed before:
279284
[closure expressions]: expressions/closure-expr.html
280285
[enum variant]: expressions/enum-variant-expr.html
281286
[field]: expressions/field-expr.html
287+
[functional update]: expressions/struct-expr.html#functional-update-syntax
282288
[grouped]: expressions/grouped-expr.html
283289
[literals]: expressions/literal-expr.html
284290
[match]: expressions/match-expr.html
@@ -316,6 +322,7 @@ They are never allowed before:
316322
[let statement]: statements.html#let-statements
317323
[Mutable `static` items]: items/static-items.html#mutable-statics
318324
[const contexts]: const_eval.html
325+
[scrutinee]: glossary.html#scrutinee
319326
[slice]: types/slice.html
320327
[statement]: statements.html
321328
[static variables]: items/static-items.html

src/expressions/struct-expr.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,23 @@ colon.
5656
A value of a [union] type can also be created using this syntax, except that it must
5757
specify exactly one field.
5858

59+
## Functional update syntax
60+
5961
A struct expression can terminate with the syntax `..` followed by an
6062
expression to denote a functional update. The expression following `..` (the
61-
base) must have the same struct type as the new struct type being formed. The
62-
entire expression denotes the result of constructing a new struct (with the
63-
same type as the base expression) with the given values for the fields that
64-
were explicitly specified and the values in the base expression for all other
65-
fields. Just as with all struct expressions, all of the fields of the struct
66-
must be [visible], even those not explicitly named.
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. Just as with
67+
all struct expressions, all of the fields of the struct must be [visible], even
68+
those not explicitly named.
6769

6870
```rust
6971
# struct Point3d { x: i32, y: i32, z: i32 }
70-
let base = Point3d {x: 1, y: 2, z: 3};
71-
Point3d {y: 0, z: 10, .. base};
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);
7276
```
7377

7478
Struct expressions with curly braces can't be used directly in a [loop] or [if]

0 commit comments

Comments
 (0)