Function understand_drop

Source
pub fn understand_drop()
Expand description

示例1: Move 的本质:drop 标记

fn main(){
    // impl Copy for i32
    let mut a = "42".to_string();
    let b = a; // drop(a);

    a = "32".to_string();
    println!("{:?}", a);
}

示例二:Drop 析构函数

struct PrintDrop(&'static str);
impl Drop for PrintDrop {
    fn drop(&mut self) {
        println!("Dropping {}", self.0)
    }
}
fn main() {
    let x = PrintDrop("x");
    let y = PrintDrop("y");
}

元组:

struct PrintDrop(&'static str);
    impl Drop for PrintDrop {
        fn drop(&mut self) {
            println!("Dropping {}", self.0)
    }
}
fn main() {
    let tup1 = (PrintDrop("a"), PrintDrop("b"), PrintDrop("c"));
    let tup2 = (PrintDrop("x"), PrintDrop("y"), PrintDrop("z"));
}

带panic的元组:

struct PrintDrop(&'static str);
impl Drop for PrintDrop {
    fn drop(&mut self) {
        println!("Dropping {}", self.0)
}
}
fn main() {
    let tup1 = (PrintDrop("a"), PrintDrop("b"), PrintDrop("c"));
    let tup2 = (PrintDrop("x"), PrintDrop("y"), panic!());
}

结构体:

struct PrintDrop(&'static str);

impl Drop for PrintDrop {
    fn drop(&mut self) {
        println!("Dropping {}", self.0)
    }
}

struct Foo {
    bar: PrintDrop,
    baz: PrintDrop,
}

impl Drop for Foo {
    fn drop(&mut self) {
        println!("Dropping Foo")
    }
}

fn main() {
    let foo = Foo {
        bar: PrintDrop("bar"),
        baz: PrintDrop("baz"),
    };
}

闭包:

struct PrintDrop(&'static str);
impl Drop for PrintDrop {
    fn drop(&mut self) {
        println!("Dropping {}", self.0)
    }
}
fn main() {
    let z = PrintDrop("z");
    let x = PrintDrop("x");
    let y = PrintDrop("y");
    let closure = move || { y; z; x; };
}

闭包修改变量:

struct PrintDrop(&'static str);
impl Drop for PrintDrop {
    fn drop(&mut self) {
        println!("Dropping {}", self.0)
    }
}
fn main() {
    let y = PrintDrop("y");
    let x = PrintDrop("x");
    let z = PrintDrop("z");
    let closure = move || {
        { let z_ref = &z; }
        x; y; z;
    };
}

示例三: 所有权 forget/ ManuallyDrop

// https://doc.rust-lang.org/src/alloc/sync.rs.html#319
impl<T> Arc<T> {
    pub fn new(data: T) -> Arc<T> {
        // Start the weak pointer count as 1 which is the weak pointer that's
        // held by all the strong pointers (kinda), see std/rc.rs for more info
        let x: Box<_> = box ArcInner {
            strong: atomic::AtomicUsize::new(1),
            weak: atomic::AtomicUsize::new(1),
            data,
        };
        // ManuallyDrop
        Self::from_inner(Box::leak(x).into())
    }

    // ...
}

impl<T> Weak<T> {
    pub fn into_raw(self) -> *const T {
        let result = self.as_ptr();
        mem::forget(self);
        result
    }
}