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/destructors.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -266,6 +266,9 @@ smallest scope that contains the expression and is one of the following:
266
266
> [!NOTE]
267
267
> The [scrutinee] of a `match` expression is not a temporary scope, so temporaries in the scrutinee can be dropped after the `match` expression. For example, the temporary for `1` in `match 1 { ref mut z => z };` lives until the end of the statement.
268
268
269
+
> [!NOTE]
270
+
> The desugaring of a [destructuring assignment] restricts the temporary scope of its assigned value operand (the RHS). For details, see [expr.assign.destructure.tmp-scopes].
271
+
269
272
r[destructors.scope.temporary.edition2024]
270
273
> [!EDITION-2024]
271
274
> The 2024 edition added two new temporary scope narrowing rules: `if let` temporaries are dropped before the `else` block, and temporaries of tail expressions of blocks are dropped immediately after the tail expression is evaluated.
@@ -485,6 +488,9 @@ expression which is one of the following:
485
488
* The final expression of an extending [`if`] expression's consequent, `else if`, or `else` block.
486
489
* An arm expression of an extending [`match`] expression.
487
490
491
+
> [!NOTE]
492
+
> The desugaring of a [destructuring assignment] makes its assigned value operand (the RHS) an extending expression within a newly-introduced block. For details, see [expr.assign.destructure.tmp-ext].
493
+
488
494
So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some(&mut 3)`
489
495
are all extending expressions. The borrows in `&0 + &1` and `f(&mut 0)` are not.
490
496
@@ -640,6 +646,7 @@ There is one additional case to be aware of: when a panic reaches a [non-unwindi
Note that default binding modes do not apply for the desugared expression.
864
864
865
+
r[expr.assign.destructure.tmp-scopes]
866
+
> [!NOTE]
867
+
> The desugaring restricts the [temporary scope] of the assigned value operand (the RHS) of a destructuring assignment.
868
+
>
869
+
> In a basic assignment, the [temporary] is dropped at the end of the enclosing temporary scope. Below, that's the statement. Therefore, the assignment and use is allowed.
> However, ifwetrytouse `x`, even within the same statement, we'll get an error because the [temporary] is dropped at the end of this introduced block.
922
+
>
923
+
> ```rust,compile_fail,E0716
924
+
> # fn temp() {}
925
+
> # let x;
926
+
> ([x] = [&temp()], x); // ERROR
927
+
> ```
928
+
>
929
+
> Thisdesugarsto:
930
+
>
931
+
> ```rust,compile_fail,E0716
932
+
> # fntemp() {}
933
+
> # letx;
934
+
> (
935
+
> {
936
+
> let [_x] = [&temp()];
937
+
> x=_x;
938
+
> }, // <-- The temporary is dropped here.
939
+
> x, // ERROR
940
+
> );
941
+
> ```
942
+
865
943
r[expr.compound-assign]
866
944
## Compoundassignmentexpressions
867
945
@@ -1011,6 +1089,7 @@ As with normal assignment expressions, compound assignment expressions always pr
0 commit comments