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:
<details> <summary>Click here to reveal hints</summary>- 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.
use std::path::PathBuf;pub struct TempFile {path: PathBuf,}impl TempFile {// 1. Define the `new` associated function}impl Drop for TempFile {fn drop(&mut self) {// Your code here to delete the file when TempFile is droppedunimplemented!()}}// Example usagepub fn main() {let file_path = PathBuf::from("example_temp_file.tmp");let tempfile =TempFile::new(file_path.to_str().unwrap()).expect("Failed to create temporary file");assert!(tempfile.path.exists(), "File does not exist");drop(tempfile);assert!(!file_path.exists(), "File was not deleted");let tempfile_2 = TempFile::new(&String::from("example_temp_file_2.tmp")).expect("Failed to create temporary file");drop(tempfile_2);}