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
+63-4
Original file line number
Diff line number
Diff line change
@@ -428,13 +428,15 @@ 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 *assignee operand*, followed by an equals sign (`=`) and a [value expression], the *assigned value operand*.
432
+
In its most basic form, an assignee expression is a [place expression], and we discuss this case first.
433
+
The more general case of destructuring assignment is discussed below, but this case always decomposes into sequential assignments to place expressions, which may be considered the more fundamental case.
432
434
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.
435
+
### Basic assignments
435
436
436
437
Evaluating assignment expressions begins by evaluating its operands.
437
-
The assigned value operand is evaluated first, followed by the assigned place operand.
438
+
The assigned value operand is evaluated first, followed by the assignee expression.
439
+
For destructuring assignment, subexpressions of the assignee expression are evaluated left-to-right.
438
440
439
441
> **Note**: This is different than other expressions in that the right operand is evaluated before the left one.
440
442
@@ -451,6 +453,60 @@ let y = 0;
451
453
x=y;
452
454
```
453
455
456
+
### Destructuring assignments
457
+
458
+
Destructuring assignment is a counterpart to destructuring pattern matches for variable declaration, permitting assignment to complex values, such as tuples or structs.
459
+
For instance, we may swap two mutable variables:
460
+
461
+
```rust
462
+
let (muta, mutb) = (0, 1);
463
+
// Swap `a` and `b` using destructuring assignment.
464
+
(b, a) = (a, b);
465
+
```
466
+
467
+
In contrast to destructuring declarations using `let`, patterns may not appear on the left-hand side of an assignment due to syntactic ambiguities.
468
+
Instead, a group of expressions that correspond to patterns are designated to be [assignee expressions][assignee expression], and permitted on the left-hand side of an assignment.
469
+
Assignee expressions are then desugared to pattern matches followed by sequential assignment.
470
+
The desugared patterns must be irrefutable: in particular, this means that only slice patterns whose length is known at compile-time, and the trivial slice `[..]`, are permitted for destructuring assignment.
471
+
472
+
The desugaring method is straightforward, and is illustrated best by example.
473
+
474
+
```rust
475
+
# structStruct { x:u32, y:u32 }
476
+
# let (muta, mutb) = (0, 0);
477
+
(a, b) = (3, 4);
478
+
479
+
[a, b] = [3, 4];
480
+
481
+
Struct { x:a, y:b } =Struct { x:3, y:4};
482
+
483
+
// desugars to:
484
+
485
+
{
486
+
let (_a, _b) = (3, 4);
487
+
a=_a;
488
+
b=_b;
489
+
}
490
+
491
+
{
492
+
let [_a, _b] = [3, 4];
493
+
a=_a;
494
+
b=_b;
495
+
}
496
+
497
+
{
498
+
letStruct { x:_a, y:_b } =Struct { x:3, y:4};
499
+
a=_a;
500
+
b=_b;
501
+
}
502
+
```
503
+
504
+
Identifiers are not forbidden from being used multiple times in a single assignee expression.
505
+
506
+
[Underscore expressions][_UnderscoreExpression_] and empty [range expressions][_RangeExpression_] may be used to ignore certain values, without binding them.
507
+
508
+
Note that default binding modes do not apply for the desugared expression.
509
+
454
510
## Compound assignment expressions
455
511
456
512
> **<sup>Syntax</sup>**\
@@ -530,6 +586,7 @@ See [this test] for an example of using this dependency.
0 commit comments