Skip to content

Commit 6536b3b

Browse files
committed
Add a correction and an example
1 parent e37d803 commit 6536b3b

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/destructors.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,6 @@ println!("{:?}", const { &PanicOnDrop::new() });
551551
r[destructors.scope.lifetime-extension.exprs.other]
552552
The extended scope of any other expression is its [temporary scope].
553553

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-
557554
> [!EXAMPLE]
558555
> In this example, the temporary value holding the result of `temp()` is extended to the end of the statement:
559556
>
@@ -573,6 +570,26 @@ The extended scope of any other expression is its [temporary scope].
573570
>
574571
> 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]).
575572
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+
576593
#### Examples
577594
578595
Here are some examples where expressions have extended temporary scopes:

0 commit comments

Comments
 (0)