t_wの輪郭

Feedlyでフォローするボタン
Rust特質

Drop

2022/3/2 20:29:00
Drop#dropあれDropトレイトで片付け時にコードを走らせる

あれ

2022/3/2 20:37:00
    #[test]
    fn test_CustomSmartPointer() {
        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.");

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