mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 04:33:27 +08:00
Updated 'src/smart_pointers/drop-t'.
This commit is contained in:
6
projects/drop-example/Cargo.toml
Normal file
6
projects/drop-example/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "drop-example"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
18
projects/drop-example/src/main.rs
Normal file
18
projects/drop-example/src/main.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
struct CustomSmartPointer {
|
||||
data: String,
|
||||
}
|
||||
|
||||
impl Drop for CustomSmartPointer {
|
||||
fn drop(&mut self) {
|
||||
println! ("正在以数据 `{}` 弃用 CustomSmartPointer!", self.data);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let c = CustomSmartPointer {
|
||||
data: String::from("我的事情"),
|
||||
};
|
||||
println! ("CustomSmartPointer 实例已创建");
|
||||
drop(c);
|
||||
println! ("CustomSmartPointer 在 main 结束前被弃用");
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "drop_func_demo"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -1,19 +0,0 @@
|
||||
struct CustomSmartPointer {
|
||||
data: String,
|
||||
}
|
||||
|
||||
impl Drop for CustomSmartPointer {
|
||||
fn drop(&mut self) {
|
||||
println! ("正在使用数据 `{}` 弃用 CustomSmartPointer!", self.data);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let c = CustomSmartPointer {
|
||||
data: String::from("我的事情"),
|
||||
};
|
||||
|
||||
println! ("已创建出一个 CustomSmartPointer 实例。");
|
||||
drop(c);
|
||||
println! ("在 main 结束之前这个 CustomSmartPointer 已被弃用。")
|
||||
}
|
||||
Reference in New Issue
Block a user