[][src]Function tao_of_rust::ch04::raii::drop_demo

pub fn drop_demo()

RAII: 确定性析构

Base usage: 实现Drop

use std::ops::Drop;
#[derive(Debug)]
struct S(i32);
impl Drop for S {
    fn drop(&mut self) {
        println!("drop {}", self.0);
    }
}
fn main() {
    let x = S(1);
    println!("crate x: {:?}", x);
    {
        let y = S(2);
        println!("crate y: {:?}", y);
        println!("exit inner scope");
    }
    println!("exit main");
}Run

Base usage: 配合Valgrind工具来检查是否会内存泄漏 看看Box是否会自动释放

fn create_box() {
    let box3 = Box::new(3);
}
fn main() {
    let box1 = Box::new(1);
    {
        let box2 = Box::new(2);
    }
    for _ in 0..1_000 {
       create_box();
   }
}Run

Base usage: 使用花括号块主动析构

fn main() {
    let mut v = vec![1, 2, 3];
    {
        v
    };
    //   v.push(4);
}Run

Base usage: 变量遮蔽不等于drop

use std::ops::Drop;
#[derive(Debug)]
struct S(i32);
impl Drop for S {
    fn drop(&mut self) {
        println!("drop for {}", self.0);
    }
}
fn main() {
   let x = S(1);
   println!("create x: {:?}", x);
   let x = S(2);
   println!("create shadowing x: {:?}", x);
}Run