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 exact rules for temporary lifetime extension are subject to change. This is describing the current behavior only.
377
377
378
-
r[destructors.scope.lifetime-extension.let]
379
-
The temporary scopes for expressions in `let` statements are sometimes
380
-
*extended* to the scope of the block containing the `let` statement. This is
381
-
done when the usual temporary scope would be too small, based on certain
382
-
syntactic rules. For example:
378
+
r[destructors.scope.lifetime-extension.intro]
379
+
The temporary scopes for expressions are sometimes *extended*. This is done when the usual temporary scope would be too small, based on certain syntactic rules. For example:
383
380
384
381
```rust
385
382
letx=&mut0;
@@ -388,18 +385,6 @@ let x = &mut 0;
388
385
println!("{}", x);
389
386
```
390
387
391
-
r[destructors.scope.lifetime-extension.static]
392
-
Lifetime extension also applies to `static` and `const` items, where it
393
-
makes temporaries live until the end of the program. For example:
394
-
395
-
```rust
396
-
constC:&Vec<i32> =&Vec::new();
397
-
// Usually this would be a dangling reference as the `Vec` would only
398
-
// exist inside the initializer expression of `C`, but instead the
399
-
// borrow gets lifetime-extended so it effectively has `'static` lifetime.
If a [borrow], [dereference][dereference expression], [field][field expression], or [tuple indexing expression] has an extended temporary scope, then so does its operand. If an [indexing expression] has an extended temporary scope, then the indexed expression also has an extended temporary scope.
405
390
@@ -445,7 +430,7 @@ So `ref x`, `V(ref x)` and `[ref x, y]` are all extending patterns, but `x`, `&r
If a temporary scope is extended through the scope of an extending expression, it is extended through that scope's [parent][destructors.scope.nesting].
509
494
510
495
r[destructors.scope.lifetime-extension.exprs.let]
511
-
A temporary scope extended through a `let` statement scope is [extended] to the scope of the block containing the `let` statement ([destructors.scope.lifetime-extension.let]).
496
+
A temporary scope extended through a `let` statement scope is [extended] to the scope of the block containing the `let` statement.
A temporary scope extended through a [static][static item] or [constant item] scope or a [const block][const block expression] scope is [extended] to the end of the program ([destructors.scope.lifetime-extension.static]).
499
+
A temporary scope extended through a [static][static item] or [constant item] scope or a [const block][const block expression] scope is [extended] to the end of the program.
500
+
501
+
```rust
502
+
constC:&Vec<i32> =&Vec::new();
503
+
// Usually this would be a dangling reference as the `Vec` would only
504
+
// exist inside the initializer expression of `C`, but instead the
505
+
// borrow gets lifetime-extended so it effectively has `'static` lifetime.
0 commit comments