Skip to content

Make note of interaction between let _ and Drop #26138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/doc/trpl/drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,38 @@ BOOM times 1!!!
```

The TNT goes off before the firecracker does, because it was declared
afterwards. Last in, first out.
afterwards. Last in, first out. There’s one exception to this, however:
a binding to `_` will be dropped immediately:

```rust
struct Noisy(u8);

impl Drop for Noisy {
fn drop(&mut self) { println!("dropping {}", self.0); }
}

fn main() {
let _ = Noisy(1);
let x = Noisy(2);
let y = Noisy(3);
}
```

Will print:

```text
dropping 1
dropping 3
dropping 2
```

rather than

```text
dropping 3
dropping 2
dropping 1
```

So what is `Drop` good for? Generally, `Drop` is used to clean up any resources
associated with a `struct`. For example, the [`Arc<T>` type][arc] is a
Expand Down