4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RustのDrop traitの確認

Posted at

コード:

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
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?