Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b60c3e2
Closes #15919
bombless Apr 30, 2015
4c8b813
Closes #15919
bombless Apr 30, 2015
9b3d315
std: Update crate docs
brson May 8, 2015
4b52059
Add error explanations for E0049, E0050, E0069, E0106, E0107, E0166.
May 11, 2015
4fee8d3
Add error explanation for E0066.
May 12, 2015
c3dc769
Add error explanation for E0322.
May 12, 2015
71f6ed8
Remove comment description of E0234.
May 12, 2015
cbeda68
Add error explanation for E0201.
May 12, 2015
46753da
Record correct span for static mut items
GSam May 12, 2015
a22b327
book: typo fixes, wording improvements.
wheals May 12, 2015
6f3701d
readme: Rework the arch support matrix
richo May 12, 2015
6ebba71
doc: Address feedback
brson May 12, 2015
33a30f2
Improve error explanations for E0049, E0050, E0166.
May 12, 2015
5c77f0d
Remove extended explanation of lifetime elision from E0106.
May 12, 2015
0ad15bb
TRPL: release channels
steveklabnik May 7, 2015
9a3e98b
TRPL: Drop
steveklabnik May 11, 2015
fc6372e
TRPL: Rust inside other languages
steveklabnik May 12, 2015
2ba6169
TRPL: dining philosophers
steveklabnik May 11, 2015
457aed7
trpl: move tuple-structs.md into structs.md
geofft May 12, 2015
797d8e2
trpl/structs: Document unit-like structs
geofft May 12, 2015
8d50216
trpl/enums: Rewrite
geofft May 12, 2015
f59f41e
trpl/match: Add an example for matching on enums
geofft May 13, 2015
d3dd694
Rollup merge of #24987 - bombless:large-array, r=pnkfelix
steveklabnik May 13, 2015
6834d37
Rollup merge of #25224 - brson:stddoc, r=steveklabnik
steveklabnik May 13, 2015
273224b
Rollup merge of #25317 - steveklabnik:trpl_drop, r=alexcrichton
steveklabnik May 13, 2015
8f16862
Rollup merge of #25321 - steveklabnik:second_tutorial, r=alexcrichton
steveklabnik May 13, 2015
b833737
Rollup merge of #25324 - richo:richo-compat, r=huonw
steveklabnik May 13, 2015
cd1f94a
Rollup merge of #25328 - nham:E0106_E0107, r=alexcrichton
steveklabnik May 13, 2015
2b1a1ef
Rollup merge of #25333 - GSam:master, r=nrc
steveklabnik May 13, 2015
b53392c
Merge branch 'doc_release_channels' into rollup
steveklabnik May 13, 2015
80381c3
Merge branch 'doc-fixes' of https://github.com/wheals/rust into rollup
steveklabnik May 13, 2015
2c01eac
Merge branch 'trpl_embedding' into rollup
steveklabnik May 13, 2015
758d568
Merge branch 'trpl-fix-enums' of https://github.com/geofft/rust into …
steveklabnik May 13, 2015
1d34f92
Update error messages for tests in compile-fail for E0066 and E0069.
May 13, 2015
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
Prev Previous commit
Next Next commit
TRPL: Drop
  • Loading branch information
steveklabnik committed May 12, 2015
commit 9a3e98be1c20781e566d12d14940c8dd3d237b15
68 changes: 66 additions & 2 deletions src/doc/trpl/drop.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
% `Drop`
% Drop

Coming soon!
Now that we’ve discussed traits, let’s talk about a particular trait provided
by the Rust standard library, [`Drop`][drop]. The `Drop` trait provides a way
to run some code when a value goes out of scope. For example:

[drop]: ../std/ops/trait.Drop.html

```rust
struct HasDrop;

impl Drop for HasDrop {
fn drop(&mut self) {
println!("Dropping!");
}
}

fn main() {
let x = HasDrop;

// do stuff

} // x goes out of scope here
```

When `x` goes out of scope at the end of `main()`, the code for `Drop` will
run. `Drop` has one method, which is also called `drop()`. It takes a mutable
reference to `self`.

That’s it! The mechanics of `Drop` are very simple, but there are some
subtleties. For example, values are dropped in the opposite order they are
declared. Here’s another example:

```rust
struct Firework {
strength: i32,
}

impl Drop for Firework {
fn drop(&mut self) {
println!("BOOM times {}!!!", self.strength);
}
}

fn main() {
let firecracker = Firework { strength: 1 };
let tnt = Firework { strength: 100 };
}
```

This will output:

```text
BOOM times 100!!!
BOOM times 1!!!
```

The TNT goes off before the firecracker does, because it was declared
afterwards. Last in, first out.

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
reference-counted type. When `Drop` is called, it will decrement the reference
count, and if the total number of references is zero, will clean up the
underlying value.

[arc]: ../std/sync/struct.Arc.html