From 0c6091d677c970f57bd18b3edc09b440014f8621 Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Sun, 20 Sep 2020 17:21:47 +0800 Subject: Drop dead instances --- smart_pointer/src/main.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'smart_pointer/src') diff --git a/smart_pointer/src/main.rs b/smart_pointer/src/main.rs index d7ed84c..d0be410 100644 --- a/smart_pointer/src/main.rs +++ b/smart_pointer/src/main.rs @@ -22,6 +22,16 @@ impl Deref for MyBox { } } +struct CustomSmartPointer { + data: String, +} + +impl Drop for CustomSmartPointer { + fn drop(&mut self) { + println!("Dropping CustomSmartPointer with data `{}`!", self.data); + } +} + fn main() { // Storing `i32` value on heap let b = Box::new(5); @@ -59,4 +69,14 @@ fn main() { hello(&m); // `m`: MyBox, `*m`: String, `(*m)[..]`: str, `&(*m)[..]`: &str hello(&(*m)[..]); + + let c = CustomSmartPointer { + data: String::from("my stuff"), + }; + let d = CustomSmartPointer { + data: String::from("other stuff"), + }; + println!("CustomSmartPointer created."); + drop(c); + println!("CustomSmartPointer dropped before the end of main."); } -- cgit v1.2.3