Crate tempdir [] [src]

Temporary directories of files.

The TempDir type creates a directory on the file system that is deleted once it goes out of scope. At construction, the TempDir creates a new directory with a randomly generated name and a prefix of your choosing.


extern crate tempdir;

use std::fs::File;
use std::io::Write;
use tempdir::TempDir;

fn main() {
    // Create a directory inside of `std::env::temp_dir()`, named with
    // the prefix "example".
    let tmp_dir = TempDir::new("example").expect("create temp dir");
    let file_path = tmp_dir.path().join("my-temporary-note.txt");
    let mut tmp_file = File::create(file_path).expect("create temp file");
    writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");

    // By closing the `TempDir` explicitly, we can check that it has
    // been deleted successfully. If we don't close it explicitly,
    // the directory will still be deleted when `tmp_dir` goes out
    // of scope, but we won't know whether deleting the directory
    // succeeded.
    tmp_dir.close().expect("delete temp dir");



A directory in the filesystem that is automatically deleted when it goes out of scope.