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
The extended scope of any other expression is its [temporary scope].
553
553
554
-
> [!NOTE]
555
-
> In this case, the expression is not extending, meaning it cannot be a borrow expression or a [super operand][super operands] to a [super macro call] expression, so its temporary scope is given by [destructors.scope.temporary.enclosing].
556
-
557
554
> [!EXAMPLE]
558
555
> In this example, the temporary value holding the result of `temp()` is extended to the end of the statement:
559
556
>
@@ -573,6 +570,26 @@ The extended scope of any other expression is its [temporary scope].
573
570
>
574
571
> If not for temporary lifetime extension, the result of `temp()` would be dropped after evaluating the tail expression of the block `{ &temp() }` ([destructors.scope.temporary.enclosing]).
575
572
573
+
> [!EXAMPLE]
574
+
> In this example, the temporary value holding the result of `temp()` is extended to the end of the block:
575
+
>
576
+
> ```rust,edition2024
577
+
> # fn temp() {}
578
+
> let x = &*&temp();
579
+
> # x;
580
+
> ```
581
+
>
582
+
> `temp()` is the operand of a borrow expression, so its temporary scope is its extended scope.
583
+
> To determine its extended scope, look outward:
584
+
>
585
+
> * Since borrow expressions' operands are extending, the extended scope of `temp()` is the extended scope of its extending parent, the borrow expression.
586
+
> * `&temp()` is the operand of a [dereference expression], which is not extending, so its extended scope is its temporary scope.
587
+
> * Per [destructors.scope.lifetime-extension.sub-expressions], the temporary scope of `&temp()` is the temporary scope of the dereference expression, `*&temp()`.
588
+
> * `*&temp()` is the operand of a borrow expression, so its temporary scope is its extended scope. Since borrow expressions' operands are extending, this is the extended scope of `&*&temp()`.
589
+
> * `&*&temp()` is the initializer of a `let` statement, so its extended scope, and thus the extended scope of `temp()`, is the scope of the block containing the `let` statement.
590
+
>
591
+
> If not for temporary lifetime extension, the result of `temp()` would be dropped at the end of the statement ([destructors.scope.temporary.enclosing]).
592
+
576
593
#### Examples
577
594
578
595
Here are some examples where expressions have extended temporary scopes:
0 commit comments