1
Fork 0

Add example of recursive drop to Drop trait.

This commit is contained in:
Havvy 2017-05-22 15:06:25 -07:00
parent 81734e0e06
commit 14b767d07e

View file

@ -153,6 +153,8 @@ use marker::Unsize;
/// 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`
@ -171,6 +173,32 @@ use marker::Unsize;
/// 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 {