Add example of recursive drop to Drop trait.
This commit is contained in:
parent
81734e0e06
commit
14b767d07e
1 changed files with 28 additions and 0 deletions
|
@ -153,6 +153,8 @@ use marker::Unsize;
|
||||||
/// The `Drop` trait is used to run some code when a value goes out of scope.
|
/// The `Drop` trait is used to run some code when a value goes out of scope.
|
||||||
/// This is sometimes called a 'destructor'.
|
/// This is sometimes called a 'destructor'.
|
||||||
///
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
|
/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
|
||||||
|
@ -171,6 +173,32 @@ use marker::Unsize;
|
||||||
/// let _x = HasDrop;
|
/// 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"]
|
#[lang = "drop"]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub trait Drop {
|
pub trait Drop {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue