diff options
author | Jordan Gong <jordan.gong@protonmail.com> | 2020-09-20 17:21:47 +0800 |
---|---|---|
committer | Jordan Gong <jordan.gong@protonmail.com> | 2020-09-20 17:21:47 +0800 |
commit | 0c6091d677c970f57bd18b3edc09b440014f8621 (patch) | |
tree | 479a1efe3ea42a9d27cfca0bf64b0e46c80260e1 /smart_pointer/src | |
parent | 464864e09f465be5e3b11e96ff071038c19e623d (diff) |
Drop dead instances
Diffstat (limited to 'smart_pointer/src')
-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."); } |