The Drop Trait
The Drop trait in Rust is used to run custom cleanup logic when a value goes out of scope. This is particularly useful for managing resources like files, network connections, or any other system resources that need to be explicitly released.
When a value that implements Drop goes out of scope, the Rust runtime automatically calls the drop method, allowing you to define custom behavior for resource cleanup.
In this challenge, you will implement a struct that represents a temporary file. The file should be automatically deleted when the struct is dropped, simulating the behavior of temporary files in real-world applications.
Your Task
- Create
TempFilestruct with a methodnewthat takes a file name as input, it must accept both&strandStringtypes. - Implement the
Droptrait forTempFileto delete the file automatically when the struct goes out of scope.
Hints
If you're stuck, here are some hints to help you solve the challenge:
Click here to reveal hints
- Use
std::fs::Fileto create a temporary file. - Use
std::env::temp_dir()to get the path for temporary files. - The
std::fs::remove_filemethod can be used to delete files. - The
PathBufstruct is helpful for managing file paths. - Use the
AsRef<str>trait to allow flexible input types for the file name. - Implement the
Droptrait for custom cleanup logic.
No run yet
Press
Run to execute your code and see test results here.