Temporary directory management for Rust

License: Other

Language: Rust


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

Build Status Build status


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.


Add this to your Cargo.toml:

tempdir = "0.3"

and this to your crate root:

extern crate tempdir;


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!")?;


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.

Project Statistics

Sourcerank 16
Repository Size 580 KB
Stars 112
Forks 23
Watchers 12
Open issues 1
Dependencies 2
Contributors 17
Tags 3
Last updated
Last pushed

Top Contributors See all

Alex Crichton Brian Anderson Paul Woolcock Ashley Mannix Rob Donnelly Craig Hills Roman Michael Rybakov Austin Bonander Cldfire Jake Goulding Alex Gaynor Anders Kaseorg Igor Gnatenko XAMPPRocky Brian Smith David Tolnay

Packages Referencing this Repo

A library for managing a temporary directory and deleting all contents when it's dropped.
Latest release 0.3.7 - Updated - 112 stars

Recent Tags See all

v0.3.7 March 21, 2018
v0.3.6 January 30, 2018
0.3.5 August 01, 2016

Something wrong with this page? Make a suggestion

Last synced: 2018-06-26 21:13:55 UTC

Login to resync this repository