Implement Drop trait

main
sgoudham 3 years ago
parent 3f5d729285
commit 5ecaa30a75
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -16,6 +16,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 hello(name: &str) { fn hello(name: &str) {
println!("Hello, {}!", name); println!("Hello, {}!", name);
} }
@ -29,5 +39,14 @@ fn main() {
let m = MyBox::new("Rust"); let m = MyBox::new("Rust");
hello(&m); hello(&m);
get_name();
let c = CustomSmartPointer {
data: String::from("my stuff"),
};
let d = CustomSmartPointer {
data: String::from("other stuff"),
};
println!("CustomSmartPointers created.");
drop(c);
println!("CustomSmartPointer dropped before the end of main.");
} }
Loading…
Cancel
Save