Skip to content

Commit

Permalink
Document and show drop order
Browse files Browse the repository at this point in the history
Fixes #717.
  • Loading branch information
carols10cents committed Sep 19, 2017
1 parent d0abf1f commit 90af43b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions second-edition/src/ch15-03-drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ struct CustomSmartPointer {

impl Drop for CustomSmartPointer {
fn drop(&mut self) {
println!("Dropping CustomSmartPointer!");
println!("Dropping CustomSmartPointer with data `{}`!", self.data);
}
}

fn main() {
let c = CustomSmartPointer { data: String::from("some data") };
println!("CustomSmartPointer created.");
let c = CustomSmartPointer { data: String::from("my stuff") };
let d = CustomSmartPointer { data: String::from("other stuff") };
println!("CustomSmartPointers created.");
}
```

Expand Down Expand Up @@ -92,14 +93,17 @@ call the `drop` method explicitly.
When we run this program, we'll see the following output:

```text
CustomSmartPointer created.
Dropping CustomSmartPointer!
CustomSmartPointers created.
Dropping CustomSmartPointer with data `other stuff`!
Dropping CustomSmartPointer with data `my stuff`!
```

Rust automatically called `drop` for us when our instance went out of scope,
calling the code we specified. This is just to give you a visual guide to how
the drop method works, but usually you would specify the cleanup code that your
type needs to run rather than a print message.
calling the code we specified. Variables are dropped in the reverse order of
the order in which they were created, so `d` was dropped before `c`. This is
just to give you a visual guide to how the drop method works, but usually you
would specify the cleanup code that your type needs to run rather than a print
message.

<!-- Can you wrap this example up by saying what you would actually put in a
drop method and why?-->
Expand Down

0 comments on commit 90af43b

Please sign in to comment.