Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Drop description (src/ch15-03-drop.md) #4181

Open
wowinter13 opened this issue Dec 29, 2024 · 3 comments
Open

Improve Drop description (src/ch15-03-drop.md) #4181

wowinter13 opened this issue Dec 29, 2024 · 3 comments

Comments

@wowinter13
Copy link

Hello guys!

TLDR: my main idea is to provide something more obvious than just println!("Dropping CustomSmartPointer with data {}!", self.data);. For instance, when we are talking about files, the current book chapter states that "...lets you customize what happens when a value is about to go out of scope.", but it doesn't really show any customization behavior. So I thought it might be helpful for newcomers to see that drop() could be useful when working with fixtures, so you don't need to write a function fn clear_data() and call it manually after every test - you can just extend drop behavior.

I thought about something like this + description:

use std::fs::File;
use std::path::PathBuf;

struct TempFile {
    file: File,
    path: PathBuf,
}

impl TempFile {
    fn new(path: PathBuf) -> std::io::Result<Self> {
        Ok(Self {
            file: File::create(&path)?,
            path,
        })
    }
}

impl Drop for TempFile {
    fn drop(&mut self) {
        // Clean up the file when it's no longer needed
        if let Err(e) = std::fs::remove_file(&self.path) {
            eprintln!("Failed to remove temp file: {}", e);
        }
        println!("Dropped");
    }
}

fn main() -> std::io::Result<()> {
    let temp = TempFile::new("test.txt".into())?;
    
    // Work with temp.file here
    // The file will be automatically deleted when temp goes out of scope
    
    Ok(())
}

// OR

#[test]
fn test_with_temp_file() {
    let temp = TempFile::new("test.txt".into()).unwrap();
// print "Dropped"
}

What do you think? The example of course could be improved or changed, but what about the principal idea?

PS: I don't think this description belongs in the standard library docs (https://github.com/rust-lang/rust/blob/master/library/core/src/mem/mod.rs#L940) since this behavior is obvious, but I think it could be useful for The Rust Book.

@StefanSalewski
Copy link

That is an interesting idea, thank you very much for reporting.

My personal view is more, that describing too much details in the first book that people read about Rust makes not that much sense. I read the official Rust book in October 2023, and had the feeling that some stuff was explained already in too much detail. Rust is a complex language -- if we explain all topics in much detail, it would people take very long to learn the language. Of course we have to explain what is really needed. For the Drop trait, it is more my feeling, that understanding the principle working is important. But I would assume that most beginners will not construct data structures where they really have to implement Drop. And when they have to do so, there is now AI available: Just ask, and in seconds you get typically a very good explanation with working code examples. And we can always ask for more details if we want.

For an advanced book, your proposal might be a good idea indeed.

@wowinter13
Copy link
Author

Hi @StefanSalewski
Thanks for your response!

Do you think this description would fit better in Rust by Example?
Perhaps in this section: https://github.com/rust-lang/rust-by-example/blob/master/src/trait/drop.md
What do you think?

@StefanSalewski
Copy link

Yes, I think "Rust by example" might be a good place for your example. But there might be many other possible locations. Note that I am just a plain Rust user, so I have absolutely no influence, and maybe my feeling is just wrong. At least I can tell that I will not include such an example in my own "Rust for C-Programmers" book, as that book should remain as compact as possible, and it has already more pages than initial planed. When you add your example somewhere, it would be good to mention related crates like https://docs.rs/tempfile/latest/tempfile/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants