Temporary directory management for Rust
Clone or download
Latest commit 403f918 Apr 25, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Prepare for 0.3.7 release Mar 21, 2018
tests test: remove old fixme from the tests Oct 11, 2016
.gitignore Initial commit Mar 5, 2015
.travis.yml Tweak travis config Nov 30, 2017
Cargo.toml Prepare for 0.3.7 release Mar 21, 2018
LICENSE-APACHE Initial commit Mar 5, 2015
LICENSE-MIT Initial commit Mar 5, 2015
README.md update tempfile note Apr 19, 2018
appveyor.yml add appveyor CI Mar 29, 2017

README.md

tempdir

A Rust library for creating a temporary directory and deleting its entire contents when the directory is dropped.

Build Status Build status

Documentation

Deprecation Note

The tempdir crate is being merged into tempfile and is available in 3.x. Please direct new issues and pull requests to tempfile.

Usage

Add this to your Cargo.toml:

[dependencies]
tempdir = "0.3"

and this to your crate root:

extern crate tempdir;

Example

This sample method does the following:

  1. Create a temporary directory in the default location with the given prefix.
  2. Determine a file path in the directory and print it out.
  3. Create a file inside the temp folder.
  4. Write to the file and sync it to disk.
  5. Close the directory, deleting the contents in the process.
use std::io::{self, Write};
use std::fs::File;
use tempdir::TempDir;

fn write_temp_folder_with_files() -> io::Result<()> {
    let dir = TempDir::new("my_directory_prefix")?;
    let file_path = dir.path().join("foo.txt");
    println!("{:?}", file_path);

    let mut f = File::create(file_path)?;
    f.write_all(b"Hello, world!")?;
    f.sync_all()?;
    dir.close()?;

    Ok(())
}

Note: Closing the directory is actually optional, as it would be done on drop. The benefit of closing here is that it allows possible errors to be handled.