study_example/smart_pointer/
drop_trait.rs1use std::ops::Drop;
2struct XY {
3 x: i32,
4 y: i32,
5}
6
7struct Point {
8 xy: XY,
9 ecc: String,
10}
11
12impl Drop for XY {
13 fn drop(&mut self) {
14 println!("dropping XY:({}, {})", self.x, self.y);
15 }
16}
17
18impl Drop for Point {
19 fn drop(&mut self) {
20 println!(
21 "dropping point ecc of {} with XY:({}, {})",
22 self.ecc, self.xy.x, self.xy.y
23 );
24 }
25}
26
27fn drop_struct_test() {
41 let pointer1 = Point {
42 xy: XY { x: 1, y: 1 },
43 ecc: String::from("ecdsa"),
44 };
45 let pointer2 = Point {
46 xy: XY { x: 2, y: -1 },
47 ecc: String::from("ecdsa"),
48 };
49 }
52
53fn early_drop_with_stddrop_test() {
61 let pointer = Point {
62 xy: XY { x: 1, y: 1 },
63 ecc: String::from("ecdsa"),
64 };
65 println!("[early_drop_with_stddrop_test] pointer crated.");
66 std::mem::drop(pointer);
67 println!("[early_drop_with_stddrop_test] pointer before the end of fn");
68}
69
70pub fn drop_trait_study() {
71 drop_struct_test(); early_drop_with_stddrop_test();
73}