Skip to content

bug: Erroneous comment in Drop's TempFile inverts order of destruction #1925

Open
@AlanDeSmet

Description

@AlanDeSmet

In the page on Drop (https://doc.rust-lang.org/rust-by-example/trait/drop.html ), the TempFile example includes the following:

// When TempFile is dropped:
// 1. First, the File will be automatically closed (Drop for File)
// 2. Then our drop implementation will remove the file
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) {

I believe the comments are incorrect. I believe TempFile::drop is called before File::drop, so the file is still open when std::fs::remove_file is called.

As an additional issue, removing the file while it's still open is fine on Unix-like operating systems, but may cause problems on Windows or other operating systems that block removing opened files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions