diff options
-rw-r--r-- | smart_pointer/src/main.rs | 20 |
1 files changed, 20 insertions, 0 deletions
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<T> Deref for MyBox<T> { } } +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<String>, `*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."); } |