]> git.lizzy.rs Git - rust.git/commitdiff
Add example of recursive drop to Drop trait.
authorHavvy <ryan.havvy@gmail.com>
Mon, 22 May 2017 22:06:25 +0000 (15:06 -0700)
committerHavvy <ryan.havvy@gmail.com>
Mon, 22 May 2017 22:06:25 +0000 (15:06 -0700)
src/libcore/ops.rs

index fc3af096b183880c4b3a61910e4fe5eae73e904d..4c2d05accf389478a1d120086d3703a11b63eb2f 100644 (file)
 /// The `Drop` trait is used to run some code when a value goes out of scope.
 /// This is sometimes called a 'destructor'.
 ///
+/// 
+///
 /// # Examples
 ///
 /// A trivial implementation of `Drop`. The `drop` method is called when `_x`
 ///     let _x = HasDrop;
 /// }
 /// ```
+///
+/// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
+/// `drop` method will be called for `Outer` and then the `drop` method for
+/// `Inner` will be called. Therefore `main` prints `Dropping Outer!` and then
+/// `Dropping Inner!`.
+/// 
+/// ```
+/// struct Inner;
+/// struct Outer(Inner);
+///
+/// impl Drop for Inner {
+///     fn drop(&mut self) {
+///         println!("Dropping Inner!");
+///     }
+/// }
+///
+/// impl Drop for Outer {
+///     fn drop(&mut self) {
+///         println!("Dropping Outer!");
+///     }
+/// }
+///
+/// fn main() {
+///     let _x = Outer(Inner);
+/// }
+/// ```
 #[lang = "drop"]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub trait Drop {