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/rust-2024/temporary-tail-expr-scope.md
+23-9Lines changed: 23 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,35 +55,49 @@ In 2021 the local variable `c` is dropped before the temporary created by `c.bor
55
55
56
56
### Temporary scope may be narrowed
57
57
58
-
When a temporary is created in order to evaluate an expression, the temporary is dropped based on the [temporary scope rules]. Those rules define how long the temporary will be kept alive. Before 2024, temporaries from tail expressions of a block would be extended outside the block to the next temporary scope boundary. In many cases this would be the end of a statement or function body. In 2024, the temporaries of the tail expression may now be dropped immediately at the end of the block (before any local variables in the block).
58
+
When a temporary is created in order to evaluate an expression, the temporary is dropped based on the [temporary scope rules]. Those rules define how long the temporary will be kept alive. Before 2024, temporaries from tail expressions of a block would live past the block to the next temporary scope boundary. In many cases this would be the end of a statement or function body. In 2024, the temporaries of the tail expression may now be dropped immediately at the end of the block (before any local variables in the block).
59
59
60
60
This narrowing of the temporary scope may cause programs to fail to compile in 2024. For example:
61
61
62
62
```rust,edition2024,E0716,compile_fail
63
63
// This example works in 2021, but fails to compile in 2024.
64
64
fn main() {
65
-
let x = { &String::from("1234") }.len();
65
+
let x = { String::from("1234 ").trim() }.len();
66
66
}
67
67
```
68
68
69
-
In this example, in 2021, the temporary `String`is extended outside of the block, past the call to `len()`, and is dropped at the end of the statement. In 2024, it is dropped immediately at the end of the block, causing a compile error about the temporary being dropped while borrowed.
69
+
In this example, in 2021, the temporary `String`lives past the call to `len()` and is dropped at the end of the statement. In 2024, it is dropped immediately at the end of the block, causing a compile error about the temporary being dropped while borrowed.
70
70
71
-
The solution for these kinds of situations is to lift the block expression out to a local variable so that the temporary lives long enough:
71
+
One solution for these kinds of situations is to lift the temporary out to a local variable so that it lives long enough:
72
72
73
73
```rust,edition2024
74
74
fn main() {
75
-
let s = { &String::from("1234") };
76
-
let x = s.len();
75
+
let s = String::from("1234 ");
76
+
let x = { s.trim() }.len();
77
77
}
78
78
```
79
79
80
-
This particular example takes advantage of [temporary lifetime extension]. Temporary lifetime extension is a set of specific rules which allow temporaries to live longer than they normally would. Because the `String` temporary is behind a reference, the `String` temporary is extended long enough for the next statement to call `len()` on it.
81
-
82
80
See the [`if let` temporary scope] chapter for a similar change made to temporary scopes of `if let` expressions.
[Temporary lifetime extension] is a set of specific rules which allow temporaries to live longer than they normally would, including past block tail expressions. For example:
88
+
89
+
```rust,edition2024
90
+
fn main() {
91
+
let x = { &String::from("1234") }.len();
92
+
}
93
+
```
94
+
95
+
Because the expression evaluating to the temporary `String` is the operand of a borrow expression, that temporary's scope may be extended.
96
+
In this case, it appears syntactically in an [extending] position within the block, so the temporary `String` is extended outside of the block, past the call to `len()`.
97
+
Since the block does not however appear in an extending position for the `let` statement as a whole, the temporary `String` is not extended further and is dropped at the end of the statement.
0 commit comments