[][src]Function tempfile::tempdir

pub fn tempdir() -> Result<TempDir>

Create a new temporary directory.

The tempdir function creates a directory in the file system and returns a TempDir. The directory will be automatically deleted when the TempDirs destructor is run.

Resource Leaking

See the resource leaking docs on TempDir.


If the directory can not be created, Err is returned.


use tempfile::tempdir;
use std::fs::File;
use std::io::{self, Write};

// Create a directory inside of `std::env::temp_dir()`
let dir = tempdir()?;

let file_path = dir.path().join("my-temporary-note.txt");
let mut file = File::create(file_path)?;
writeln!(file, "Brian was here. Briefly.")?;

// `tmp_dir` goes out of scope, the directory as well as
// `tmp_file` will be deleted here.