@@ -153,6 +153,13 @@ use marker::Unsize;
153153/// The `Drop` trait is used to run some code when a value goes out of scope.
154154/// This is sometimes called a 'destructor'.
155155///
156+ /// When a value goes out of scope, if it implements this trait, it will have
157+ /// its `drop` method called. Then any fields the value contains will also
158+ /// be dropped recursively.
159+ ///
160+ /// Because of the recursive dropping, you do not need to implement this trait
161+ /// unless your type needs its own destructor logic.
162+ ///
156163/// # Examples
157164///
158165/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
@@ -171,6 +178,43 @@ use marker::Unsize;
171178/// let _x = HasDrop;
172179/// }
173180/// ```
181+ ///
182+ /// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
183+ /// `drop` method will be called first for `Outer`, then for `Inner`. Therefore
184+ /// `main` prints `Dropping Outer!` and then `Dropping Inner!`.
185+ ///
186+ /// ```
187+ /// struct Inner;
188+ /// struct Outer(Inner);
189+ ///
190+ /// impl Drop for Inner {
191+ /// fn drop(&mut self) {
192+ /// println!("Dropping Inner!");
193+ /// }
194+ /// }
195+ ///
196+ /// impl Drop for Outer {
197+ /// fn drop(&mut self) {
198+ /// println!("Dropping Outer!");
199+ /// }
200+ /// }
201+ ///
202+ /// fn main() {
203+ /// let _x = Outer(Inner);
204+ /// }
205+ /// ```
206+ ///
207+ /// Because variables are dropped in the reverse order they are declared,
208+ /// `main` will print `Declared second!` and then `Declared first!`.
209+ ///
210+ /// ```
211+ /// struct PrintOnDrop(&'static str);
212+ ///
213+ /// fn main() {
214+ /// let _first = PrintOnDrop("Declared first!");
215+ /// let _second = PrintOnDrop("Declared second!");
216+ /// }
217+ /// ```
174218#[ lang = "drop" ]
175219#[ stable( feature = "rust1" , since = "1.0.0" ) ]
176220pub trait Drop {
0 commit comments