From a4655ea6071a8bdd127916790ec0431b72bf8465 Mon Sep 17 00:00:00 2001 From: streamatron/ragebaiter Date: Tue, 31 Mar 2026 18:40:26 -0700 Subject: [PATCH] fix: correct destruction order comments in Drop TempFile example (#1925) The comments incorrectly stated that File's drop runs before the custom drop implementation. In Rust, the custom drop method runs first, and then fields are dropped afterwards. Updated comments to reflect the actual destruction order. --- src/trait/drop.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/trait/drop.md b/src/trait/drop.md index 353edd0a23..21f6df5ee5 100644 --- a/src/trait/drop.md +++ b/src/trait/drop.md @@ -76,15 +76,19 @@ impl TempFile { } // When TempFile is dropped: -// 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. +// 1. First, our custom drop implementation runs. The file is still open at this point, +// but we can remove it from the filesystem by path. +// 2. Then, after our drop returns, Rust automatically drops each field, +// so File's drop runs and closes the file handle. impl Drop for TempFile { fn drop(&mut self) { + // Note: the File is still open here — field destructors run after this method. 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. + // After this method returns, Rust will drop each field (including `file`), + // which closes the underlying file handle. } }