Use the tempfile crate to create temporary files and directories that automatically delete themselves when dropped. Add tempfile to your Cargo.toml dependencies, then use NamedTempFile::new() for files or tempdir() for directories; the OS cleans them up when the variable goes out of scope.
use tempfile::{NamedTempFile, tempdir};
fn main() -> std::io::Result<()> {
let file = NamedTempFile::new()?;
let dir = tempdir()?;
// Use file.path() and dir.path() here
Ok(())
}