Skip to content

Commit 4b945fd

Browse files
author
lukaramu
committed
Revise Drop docs
Part of rust-lang#29365. * Removed "stuttering" in summary sentence. * Copy-edited the explanaition sections * Added sub-headings in Examples section to aid linking * Actually implement `Drop` in the `PrintOnDrop` exampl * Add link to Drop chapter in TRPL * Changed `drop` summary sentence to be in 3rd person singular * Added missing link to `panic!`
1 parent 5990be5 commit 4b945fd

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/libcore/ops/drop.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,27 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/// The `Drop` trait is used to run some code when a value goes out of scope.
11+
/// Used to run some code when a value goes out of scope.
1212
/// This is sometimes called a 'destructor'.
1313
///
14-
/// When a value goes out of scope, if it implements this trait, it will have
15-
/// its `drop` method called. Then any fields the value contains will also
14+
/// When a value goes out of scope, it will have its `drop` method called if
15+
/// its type implements `Drop`. Then, any fields the value contains will also
1616
/// be dropped recursively.
1717
///
18-
/// Because of the recursive dropping, you do not need to implement this trait
18+
/// Because of this recursive dropping, you do not need to implement this trait
1919
/// unless your type needs its own destructor logic.
2020
///
21+
/// Refer to [the chapter on `Drop` in *The Rust Programming Language*][book]
22+
/// for some more elaboration.
23+
///
24+
/// [book]: ../../book/second-edition/ch15-03-drop.html
25+
///
2126
/// # Examples
2227
///
23-
/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
24-
/// goes out of scope, and therefore `main` prints `Dropping!`.
28+
/// ## Implementing `Drop`
29+
///
30+
/// The `drop` method is called when `_x` goes out of scope, and therefore
31+
/// `main` prints `Dropping!`.
2532
///
2633
/// ```
2734
/// struct HasDrop;
@@ -37,9 +44,11 @@
3744
/// }
3845
/// ```
3946
///
40-
/// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
41-
/// `drop` method will be called first for `Outer`, then for `Inner`. Therefore
42-
/// `main` prints `Dropping Outer!` and then `Dropping Inner!`.
47+
/// ## Dropping is done recursively
48+
///
49+
/// When `outer` goes out of scope, the `drop` method will be called first for
50+
/// `Outer`, then for `Inner`. Therefore, `main` prints `Dropping Outer!` and
51+
/// then `Dropping Inner!`.
4352
///
4453
/// ```
4554
/// struct Inner;
@@ -62,12 +71,20 @@
6271
/// }
6372
/// ```
6473
///
65-
/// Because variables are dropped in the reverse order they are declared,
66-
/// `main` will print `Declared second!` and then `Declared first!`.
74+
/// ## Variables are dropped in reverse order of declaration
75+
///
76+
/// `_first` is declared first and `_second` is declared second, so `main` will
77+
/// print `Declared second!` and then `Declared first!`.
6778
///
6879
/// ```
6980
/// struct PrintOnDrop(&'static str);
7081
///
82+
/// impl Drop for PrintOnDrop {
83+
/// fn drop(&mut self) {
84+
/// println!("{}", self.0);
85+
/// }
86+
/// }
87+
///
7188
/// fn main() {
7289
/// let _first = PrintOnDrop("Declared first!");
7390
/// let _second = PrintOnDrop("Declared second!");
@@ -76,24 +93,25 @@
7693
#[lang = "drop"]
7794
#[stable(feature = "rust1", since = "1.0.0")]
7895
pub trait Drop {
79-
/// A method called when the value goes out of scope.
96+
/// Executes the destructor for this type.
97+
///
98+
/// This method is called implilcitly when the value goes out of scope,
99+
/// and cannot be called explicitly (this is compiler error [E0040]).
100+
/// However, the [`std::mem::drop`] function in the prelude can be
101+
/// used to call the argument's `Drop` implementation.
80102
///
81103
/// When this method has been called, `self` has not yet been deallocated.
82-
/// If it were, `self` would be a dangling reference.
104+
/// That only happens after the method is over.
105+
/// If this wasn't the case, `self` would be a dangling reference.
83106
///
84-
/// After this function is over, the memory of `self` will be deallocated.
107+
/// # Panics
85108
///
86-
/// This function cannot be called explicitly. This is compiler error
87-
/// [E0040]. However, the [`std::mem::drop`] function in the prelude can be
88-
/// used to call the argument's `Drop` implementation.
109+
/// Given that a [`panic!`] will call `drop` as it unwinds, any [`panic!`]
110+
/// in a `drop` implementation will likely abort.
89111
///
90112
/// [E0040]: ../../error-index.html#E0040
113+
/// [`panic!`]: ../macro.panic.html
91114
/// [`std::mem::drop`]: ../../std/mem/fn.drop.html
92-
///
93-
/// # Panics
94-
///
95-
/// Given that a `panic!` will call `drop()` as it unwinds, any `panic!` in
96-
/// a `drop()` implementation will likely abort.
97115
#[stable(feature = "rust1", since = "1.0.0")]
98116
fn drop(&mut self);
99117
}

0 commit comments

Comments
 (0)