Rust的Drop Trait,相当于析构代码

mac2022-06-30  19

退出前自动执行的代码。

struct CustomSmartPointer { data: String, } impl Drop for CustomSmartPointer { fn drop(&mut self) { println!("Dropping CustomSmartPointer with data `{}`!", self.data); } } fn main() { let c = CustomSmartPointer{data: String::from("my stuff")}; println!("CustomSmartPointer created."); drop(c); println!("CustomSmartPointer dropped before the end of main."); }

转载于:https://www.cnblogs.com/aguncn/p/11440526.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)