t_wの輪郭

Feedlyでフォローするボタン

あれ

2022/3/2 20:40:00
    #[test]
    fn test_mem_drop() {
        struct CustomSmartPointer {
            data: String,
        }
        
        impl Drop for CustomSmartPointer {
            fn drop(&mut self) {
                println!("Dropping CustomSmartPointer with data `{}`!", self.data);
            }
        }
        
        let c = CustomSmartPointer {data: String::from("my stuff")};
        let d = CustomSmartPointer {data: String::from("other stuff")};
        println!("CustomSmartPointers created.");

        drop(c);

        println!("exit function!");

        //関数を抜ける際:
        //Dropping CustomSmartPointer with data `other stuff`!
        //Dropping CustomSmartPointer with data `my stuff`!
    }