From 690f3311cbaeb7b195157212722fb12ca52cc6f7 Mon Sep 17 00:00:00 2001 From: James Li Date: Fri, 29 Aug 2025 01:20:12 -0700 Subject: [PATCH 1/2] fix trait > drop example --- src/trait/drop.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/trait/drop.md b/src/trait/drop.md index 7134330c93..4a149fc920 100644 --- a/src/trait/drop.md +++ b/src/trait/drop.md @@ -6,7 +6,7 @@ resources that the implementor instance owns. `Box`, `Vec`, `String`, `File`, and `Process` are some examples of types that implement the `Drop` trait to free resources. The `Drop` trait can also be -manually implemented for any custom data type. +manually implemented for any custom data type. The following example adds a print to console to the `drop` function to announce when it is called. @@ -76,15 +76,15 @@ impl TempFile { } // When TempFile is dropped: -// 1. First, the File will be automatically closed (Drop for File) -// 2. Then our drop implementation will remove the file +// 1. First, our drop implementation will remove the file's name from the filesystem. +// 2. Then, File's drop will close the file, removing its underlying content from the disk. impl Drop for TempFile { fn drop(&mut self) { - // Note: File is already closed at this point if let Err(e) = std::fs::remove_file(&self.path) { eprintln!("Failed to remove temporary file: {}", e); } println!("> Dropped temporary file: {:?}", self.path); + // File's drop is implicitly called here because it is a field of this struct. } } From 893de44c08bea2bcd8ba553190139f639b586d9b Mon Sep 17 00:00:00 2001 From: James Li Date: Fri, 29 Aug 2025 01:24:06 -0700 Subject: [PATCH 2/2] fix stray space --- src/trait/drop.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trait/drop.md b/src/trait/drop.md index 4a149fc920..353edd0a23 100644 --- a/src/trait/drop.md +++ b/src/trait/drop.md @@ -6,7 +6,7 @@ resources that the implementor instance owns. `Box`, `Vec`, `String`, `File`, and `Process` are some examples of types that implement the `Drop` trait to free resources. The `Drop` trait can also be -manually implemented for any custom data type. +manually implemented for any custom data type. The following example adds a print to console to the `drop` function to announce when it is called.