From 680b124a0c5c62e13c7d585215bc0f5c8646481e Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Tue, 9 Jun 2015 15:37:48 -0400 Subject: [PATCH] Make note of interaction between let _ and Drop Fixes #25786 --- src/doc/trpl/drop.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/doc/trpl/drop.md b/src/doc/trpl/drop.md index 8bc25ef90d382..8c97cd4027228 100644 --- a/src/doc/trpl/drop.md +++ b/src/doc/trpl/drop.md @@ -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` type][arc] is a