summaryrefslogtreecommitdiff
path: root/smart_pointer/src/main.rs
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2020-09-20 17:21:47 +0800
committerJordan Gong <jordan.gong@protonmail.com>2020-09-20 17:21:47 +0800
commit0c6091d677c970f57bd18b3edc09b440014f8621 (patch)
tree479a1efe3ea42a9d27cfca0bf64b0e46c80260e1 /smart_pointer/src/main.rs
parent464864e09f465be5e3b11e96ff071038c19e623d (diff)
Drop dead instances
Diffstat (limited to 'smart_pointer/src/main.rs')
-rw-r--r--smart_pointer/src/main.rs20
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.");
}