走进 Rust:Drop trait
Rust 大约 603 字实现 Drop
实现Drop trait
用于手动清理某个值,例如锁。
struct CustomSmartPointer {
data: String,
}
impl Drop for CustomSmartPointer {
fn drop(&mut self) {
println!("Dropping CustomSmartPointer with data `{}`!", self.data);
}
}
调用 drop 方法
使用std::mem::drop
函数
fn main() {
let c = CustomSmartPointer { data: String::from("some data") };
println!("CustomSmartPointer created.");
drop(c);
println!("CustomSmartPointer dropped before the end of main.");
}
输出
CustomSmartPointer created.
Dropping CustomSmartPointer with data `some data`!
CustomSmartPointer dropped before the end of main.
阅读 1606 · 发布于 2020-08-25
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Kubernetes 所有 Pod 同时停机原因分析阅读 183
-
Java 普通工程 maven 打包阅读 2135
-
IntelliJ IDEA 选择 Open matching files in associated application后更改打开方式阅读 4379
-
Java 并发编程之 AtomicMarkableReference阅读 906
-
minikube start Unable to determine current user's administrator privileges阅读 165
-
MySQL 查看配置文件路径阅读 2500
-
使用 ffmpeg 剥离视频中的音频阅读 3125
-
IDEA 远程调试 Docker 容器中的 Spring Boot 程序阅读 989
-
MongoDB 过期索引(TTL 索引)阅读 3364
-
Java 中的锁 synchronized阅读 1325