I'm learning Rust by writing a simple database, I called drop(self)
in a method multiple times. While I thought the compiler would refuse to compile the code below, it compiles without warnings, but the drop(self)
seems to have no effect.
Run the code on Rust Playground
fn main() {
let database = Database::new().unwrap();
println!("-- start of drops --");
database.flush();
database.flush();
println!("-- end of main --");
}
struct Database {
}
impl Database {
fn new () -> Result<Database, std::io::Error> {
Ok(Database {
})
}
fn flush(&self) {
println!("in flush()");
drop(self);
drop(self);
}
}
impl Drop for Database {
fn drop(&mut self) {
println!("call Database drop");
let _ = self.flush();
}
}
The actual output was
-- start of drops --
in flush()
in flush()
-- end of main --
call Database drop
in flush()
What I expected (if the code compiles) was
-- start of drops --
in flush()
call Database drop
in flush()
call Database drop
...... (repeat thousands of times)
My question is: is the Rust compiler ignores the drop(self)
in the implementation of methods?
Thanks.