|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
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. |
12 | 12 | /// This is sometimes called a 'destructor'.
|
13 | 13 | ///
|
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 |
16 | 16 | /// be dropped recursively.
|
17 | 17 | ///
|
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 |
19 | 19 | /// unless your type needs its own destructor logic.
|
20 | 20 | ///
|
| 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 | +/// |
21 | 26 | /// # Examples
|
22 | 27 | ///
|
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!`. |
25 | 32 | ///
|
26 | 33 | /// ```
|
27 | 34 | /// struct HasDrop;
|
|
37 | 44 | /// }
|
38 | 45 | /// ```
|
39 | 46 | ///
|
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!`. |
43 | 52 | ///
|
44 | 53 | /// ```
|
45 | 54 | /// struct Inner;
|
|
62 | 71 | /// }
|
63 | 72 | /// ```
|
64 | 73 | ///
|
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!`. |
67 | 78 | ///
|
68 | 79 | /// ```
|
69 | 80 | /// struct PrintOnDrop(&'static str);
|
70 | 81 | ///
|
| 82 | +/// impl Drop for PrintOnDrop { |
| 83 | +/// fn drop(&mut self) { |
| 84 | +/// println!("{}", self.0); |
| 85 | +/// } |
| 86 | +/// } |
| 87 | +/// |
71 | 88 | /// fn main() {
|
72 | 89 | /// let _first = PrintOnDrop("Declared first!");
|
73 | 90 | /// let _second = PrintOnDrop("Declared second!");
|
|
76 | 93 | #[lang = "drop"]
|
77 | 94 | #[stable(feature = "rust1", since = "1.0.0")]
|
78 | 95 | 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. |
80 | 102 | ///
|
81 | 103 | /// 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. |
83 | 106 | ///
|
84 |
| - /// After this function is over, the memory of `self` will be deallocated. |
| 107 | + /// # Panics |
85 | 108 | ///
|
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. |
89 | 111 | ///
|
90 | 112 | /// [E0040]: ../../error-index.html#E0040
|
| 113 | + /// [`panic!`]: ../macro.panic.html |
91 | 114 | /// [`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. |
97 | 115 | #[stable(feature = "rust1", since = "1.0.0")]
|
98 | 116 | fn drop(&mut self);
|
99 | 117 | }
|
0 commit comments