Skip to content

Commit 690f331

Browse files
author
James Li
committed
fix trait > drop example
1 parent ad27f82 commit 690f331

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/trait/drop.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ resources that the implementor instance owns.
66

77
`Box`, `Vec`, `String`, `File`, and `Process` are some examples of types that
88
implement the `Drop` trait to free resources. The `Drop` trait can also be
9-
manually implemented for any custom data type.
9+
manually implemented for any custom data type.
1010

1111
The following example adds a print to console to the `drop` function to announce
1212
when it is called.
@@ -76,15 +76,15 @@ impl TempFile {
7676
}
7777
7878
// When TempFile is dropped:
79-
// 1. First, the File will be automatically closed (Drop for File)
80-
// 2. Then our drop implementation will remove the file
79+
// 1. First, our drop implementation will remove the file's name from the filesystem.
80+
// 2. Then, File's drop will close the file, removing its underlying content from the disk.
8181
impl Drop for TempFile {
8282
fn drop(&mut self) {
83-
// Note: File is already closed at this point
8483
if let Err(e) = std::fs::remove_file(&self.path) {
8584
eprintln!("Failed to remove temporary file: {}", e);
8685
}
8786
println!("> Dropped temporary file: {:?}", self.path);
87+
// File's drop is implicitly called here because it is a field of this struct.
8888
}
8989
}
9090

0 commit comments

Comments
 (0)