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/operator-expr.md
+76-5
Original file line number
Diff line number
Diff line change
@@ -428,13 +428,20 @@ assert_eq!(values[1], 3);
428
428
429
429
An *assignment expression* moves a value into a specified place.
430
430
431
-
An assignment expression consists of a [mutable][place expression], the *assigned place operand*, followed by an equals sign (`=`) and a [value expression], the *assigned value operand*.
431
+
An assignment expression consists of a [mutable][assignee expression], the
432
+
*assignee operand*, followed by an equals sign (`=`) and a [value expression],
433
+
the *assigned value operand*. In its most basic form, an assignee expression is
434
+
a [place expression], and we discuss this case first. The more general case of
435
+
destructuring assignment is discussed below, but this case always decomposes
436
+
into sequential assignments to place expressions, which may be considered the
437
+
more fundamental case.
432
438
433
-
Unlike other place operands, the assigned place operand must be a place expression.
434
-
Attempting to use a value expression is a compiler error rather than promoting it to a temporary.
439
+
### Basic assignments
435
440
436
-
Evaluating assignment expressions begins by evaluating its operands.
437
-
The assigned value operand is evaluated first, followed by the assigned place operand.
441
+
Evaluating assignment expressions begins by evaluating its operands. The
442
+
assigned value operand is evaluated first, followed by the assignee expression.
443
+
(For destructuring assignment, subexpressions of the assignee expression are
444
+
evaluated left-to-right.)
438
445
439
446
> **Note**: This is different than other expressions in that the right operand is evaluated before the left one.
440
447
@@ -451,6 +458,67 @@ let y = 0;
451
458
x=y;
452
459
```
453
460
461
+
### Destructuring assignments
462
+
463
+
Destructuring assignment is a counterpart to destructuring pattern matches for
464
+
variable declaration, permitting assignment to complex values, such as tuples or
465
+
structs. For instance, we may swap two mutable variables:
466
+
467
+
```rust,ignore
468
+
let (mut a, mut b) = (0, 1);
469
+
// Swap `a` and `b` using destructuring assignment.
470
+
(b, a) = (a, b);
471
+
```
472
+
473
+
In contrast to destructuring declarations using `let`, patterns may not appear
474
+
on the left-hand side of an assignment due to syntactic ambiguities. Instead, a
475
+
group of expressions that correspond to patterns are designated to be [assignee
476
+
expressions][assignee expression], and permitted on the left-hand side of an
477
+
assignment. Assignee expressions are then desugared to pattern matches followed
478
+
by sequential assignment. The desugared patterns must be irrefutable: in
479
+
particular, this means that only slice patterns whose length is known at
480
+
compile-time, and the trivial slice `[..]`, are permitted for destructuring
481
+
assignment.
482
+
483
+
The desugaring method is straightforward, and is illustrated best by example.
0 commit comments