Skip to content

Commit 2188e67

Browse files
committed
document temporary scoping for destructuring assignments
1 parent bcb96fb commit 2188e67

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/expressions/operator-expr.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,49 @@ r[expr.assign.destructure.discard-value]
862862
r[expr.assign.destructure.default-binding]
863863
Note that default binding modes do not apply for the desugared expression.
864864

865+
> [!NOTE]
866+
> Although basic assignment expressions are not [temporary scopes], the desugaring of destructuring assignments restricts the temporary scope of its assigned value operand.
867+
> For example:
868+
>
869+
> ```rust
870+
> # fn temp() {}
871+
> use std::convert::identity;
872+
>
873+
> let x;
874+
> // In a basic assignment, `temp()` is dropped at the end of the
875+
> // enclosing temporary scope, so `x` can be assigned and used
876+
> // within the same temporary scope.
877+
> (x = identity(&temp()), x);
878+
> ```
879+
>
880+
> ```rust,compile-fail,E0716
881+
> # fn temp() {}
882+
> # use std::convert::identity;
883+
> let x;
884+
> // In a destructuring assignment, `temp()` is dropped at the end of
885+
> // the `let` statement in the desugaring, so `x` cannot be assigned.
886+
> [x] = [identity(&temp())]; // ERROR
887+
> ```
888+
>
889+
> Additionally, [temporary lifetime extension] applies to the `let` statement in a desugared destructuring assignment.
890+
>
891+
> ```rust
892+
> # fn temp() {}
893+
> # use std::convert::identity;
894+
> let x;
895+
> // The temporary scope of `temp()` is extended to the end of the
896+
> // block in the desugaring, so `x` may be assigned, but it may not
897+
> // be used.
898+
> [x] = [&temp()];
899+
> ```
900+
>
901+
> ```rust,compile-fail,E0716
902+
> # fn temp() {}
903+
> # use std::convert::identity;
904+
> let x;
905+
> ([x] = [&temp()], x); // ERROR
906+
> ```
907+
865908
r[expr.compound-assign]
866909
## Compound assignment expressions
867910
@@ -1025,6 +1068,8 @@ As with normal assignment expressions, compound assignment expressions always pr
10251068
[unit]: ../types/tuple.md
10261069
[Unit-only enums]: ../items/enumerations.md#unit-only-enum
10271070
[value expression]: ../expressions.md#place-expressions-and-value-expressions
1071+
[temporary lifetime extension]: ../destructors.md#temporary-lifetime-extension
1072+
[temporary scopes]: ../destructors.md#temporary-scopes
10281073
[temporary value]: ../expressions.md#temporaries
10291074
[float-float]: https://github.com/rust-lang/rust/issues/15536
10301075
[Function pointer]: ../types/function-pointer.md

0 commit comments

Comments
 (0)