Temporary directory in Rust

TempDir

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.

Related Pages

Rocket - Single counter in a plain text file

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo