Skip to content

Commit 63b590a

Browse files
authored
Merge pull request #1592 from dingxiangfei2009/temporary-lifetime-rule-edition-2024
Add a section dedicated to Edition 2024 changes to temporary scopes
2 parents 0b9ba54 + 7d62543 commit 63b590a

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/destructors.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,22 @@ smallest scope that contains the expression and is one of the following:
211211
guard.
212212
* The body expression for a match arm.
213213
* Each operand of a [lazy boolean expression].
214+
* The pattern-matching condition and consequent body of [`if let`] ([destructors.scope.temporary.edition2024]).
215+
* The entirety of the tail expression of a block ([destructors.scope.temporary.edition2024]).
214216

215217
> **Notes**:
216218
>
217-
> Temporaries that are created in the final expression of a function
218-
> body are dropped *after* any named variables bound in the function body.
219-
> Their drop scope is the entire function, as there is no smaller enclosing temporary scope.
220-
>
221219
> The [scrutinee] of a `match` expression is not a temporary scope, so
222220
> temporaries in the scrutinee can be dropped after the `match` expression. For
223221
> example, the temporary for `1` in `match 1 { ref mut z => z };` lives until
224222
> the end of the statement.
225223
224+
r[destructors.scope.temporary.edition2024]
225+
> **Edition differences**: The 2024 edition added two new temporary scope narrowing rules: `if let` temporaries are dropped before the `else` block, and temporaries of tail expressions of blocks are dropped immediately after the tail expression is evaluated.
226+
226227
Some examples:
227228

229+
<!--TODO: edition2024 -->
228230
```rust
229231
# struct PrintOnDrop(&'static str);
230232
# impl Drop for PrintOnDrop {
@@ -242,17 +244,25 @@ if PrintOnDrop("If condition").0 == "If condition" {
242244
unreachable!()
243245
};
244246

247+
if let "if let scrutinee" = PrintOnDrop("if let scrutinee").0 {
248+
PrintOnDrop("if let consequent").0
249+
// `if let consequent` dropped here
250+
}
251+
// `if let scrutinee` is dropped here
252+
else {
253+
PrintOnDrop("if let else").0
254+
// `if let else` dropped here
255+
};
256+
245257
// Dropped before the first ||
246258
(PrintOnDrop("first operand").0 == ""
247259
// Dropped before the )
248260
|| PrintOnDrop("second operand").0 == "")
249261
// Dropped before the ;
250262
|| PrintOnDrop("third operand").0 == "";
251263

252-
// Dropped at the end of the function, after local variables.
253-
// Changing this to a statement containing a return expression would make the
254-
// temporary be dropped before the local variables. Binding to a variable
255-
// which is then returned would also make the temporary be dropped first.
264+
// Scrutinee is dropped at the end of the function, before local variables
265+
// (because this is the tail expression of the function body block).
256266
match PrintOnDrop("Matched value in final expression") {
257267
// Dropped once the condition has been evaluated
258268
_ if PrintOnDrop("guard condition").0 == "" => (),

0 commit comments

Comments
 (0)