Skip to content

Commit ca909c8

Browse files
committed
Add example of variable declaration drop order to Drop trait.
1 parent 14b767d commit ca909c8

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libcore/ops.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ use marker::Unsize;
199199
/// let _x = Outer(Inner);
200200
/// }
201201
/// ```
202+
///
203+
/// Because variables are dropped in the reverse order they are declared,
204+
/// `main` will print `Declared second!` and then `Declared first!`.
205+
///
206+
/// ```
207+
/// struct PrintOnDrop(&'static str);
208+
///
209+
/// fn main() {
210+
/// let _first = PrintOnDrop("Declared first!");
211+
/// let _second = PrintOnDrop("Declared second!");
212+
/// }
213+
/// ```
202214
#[lang = "drop"]
203215
#[stable(feature = "rust1", since = "1.0.0")]
204216
pub trait Drop {

0 commit comments

Comments
 (0)