Skip to content

Commit

Permalink
Update the example for 2024 temporary drop
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Nov 25, 2024
1 parent 4eddade commit 7d62543
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/destructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ r[destructors.scope.temporary.edition2024]
Some examples:

<!--TODO: edition2024 -->
```rust
# struct PrintOnDrop(&'static str);
# impl Drop for PrintOnDrop {
Expand All @@ -243,17 +244,25 @@ if PrintOnDrop("If condition").0 == "If condition" {
unreachable!()
};

if let "if let scrutinee" = PrintOnDrop("if let scrutinee").0 {
PrintOnDrop("if let consequent").0
// `if let consequent` dropped here
}
// `if let scrutinee` is dropped here
else {
PrintOnDrop("if let else").0
// `if let else` dropped here
};

// Dropped before the first ||
(PrintOnDrop("first operand").0 == ""
// Dropped before the )
|| PrintOnDrop("second operand").0 == "")
// Dropped before the ;
|| PrintOnDrop("third operand").0 == "";

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

0 comments on commit 7d62543

Please sign in to comment.