Drop is a #202204061235 which could be used for automatic resource clean up# when the object or variable is out of scope. It is typically implemented in #202111301656. To implement the trait Drop, we need to implement the method drop
for the structure:
impl Drop for MyStruct {
fn drop(&mut self) {
println!("Automatic resource clean up for MyStruct");
}
}
However, we can’t directly call the method drop
in order to clean the resource earlier. And there is no way for Rust to disable automatic resource clean up mechanism which even if there is, it is unwise to do so. For that purpose, use std::mem::drop
instead. (You can simply call drop({variable})
as it is included in the Rust prelude)