コード:
struct DropTest {
id: u32,
}
impl DropTest {
fn create(id: u32) -> DropTest {
DropTest { id: id }
}
}
impl Drop for DropTest {
fn drop(&mut self) {
println!("Dropping {}...", self.id);
}
}
fn sample_func(_: &DropTest, _: &DropTest) {
println!("sample_func");
}
fn main() {
println!("Start main");
{
println!("Start block");
let d = DropTest::create(1);
sample_func(&DropTest::create(2), &d);
println!("End block");
}
println!("End main");
}
結果
Start main
Start block
sample_func
Dropping 2... →呼び終わったらdrop
End block
Dropping 1... →block終わったのでdrop
End main