The tempdir crate allows us to create a temporary directory.
Dependencies
examples/temporary-folder/Cargo.toml
[package]
name = "temporary-folder"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tempdir = "0.3.7"
Code
examples/temporary-folder/src/main.rs
use tempdir::TempDir;
fn main() {
temp_dir();
}
fn temp_dir() {
let tmp_dir = TempDir::new("example").unwrap();
println!("tempdir: {:?}", tmp_dir);
}
In this example the directory is removed at when the variable goes out of scope.