File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -76,15 +76,19 @@ impl TempFile {
7676}
7777
7878// When TempFile is dropped:
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.
79+ // 1. First, our custom drop implementation runs. The file is still open at this point,
80+ // but we can remove it from the filesystem by path.
81+ // 2. Then, after our drop returns, Rust automatically drops each field,
82+ // so File's drop runs and closes the file handle.
8183impl Drop for TempFile {
8284 fn drop(&mut self) {
85+ // Note: the File is still open here — field destructors run after this method.
8386 if let Err(e) = std::fs::remove_file(&self.path) {
8487 eprintln!("Failed to remove temporary file: {}", e);
8588 }
8689 println!("> Dropped temporary file: {:?}", self.path);
87- // File's drop is implicitly called here because it is a field of this struct.
90+ // After this method returns, Rust will drop each field (including `file`),
91+ // which closes the underlying file handle.
8892 }
8993}
9094
You can’t perform that action at this time.
0 commit comments