#temporary-files #temp-dir

tempfile

A library for managing temporary files and directories

51 stable releases

3.24.0 Dec 23, 2025
3.23.0 Sep 23, 2025
3.21.0 Aug 19, 2025
3.20.0 May 11, 2025
0.5.1 May 22, 2015

#1 in Filesystem

Download history 4568367/week @ 2025-09-23 4504801/week @ 2025-09-30 4461150/week @ 2025-10-07 4390967/week @ 2025-10-14 4653494/week @ 2025-10-21 4759599/week @ 2025-10-28 4851182/week @ 2025-11-04 4612221/week @ 2025-11-11 4982995/week @ 2025-11-18 4169561/week @ 2025-11-25 4858540/week @ 2025-12-02 5172016/week @ 2025-12-09 5013486/week @ 2025-12-16 3150188/week @ 2025-12-23 3523430/week @ 2025-12-30 5789536/week @ 2026-01-06

18,405,973 downloads per month
Used in 28,656 crates (11,740 directly)

MIT/Apache

110KB
1.5K SLoC

tempfile

Crate Build Status

A secure, cross-platform, temporary file library for Rust. In addition to creating temporary files, this library also allows users to securely open multiple independent references to the same temporary file (useful for consumer/producer patterns and surprisingly difficult to implement securely).

Documentation

Usage

Minimum required Rust version: 1.63.0

Add this to your Cargo.toml:

[dependencies]
tempfile = "3"

Example

use std::fs::File;
use std::io::{Write, Read, Seek, SeekFrom};

fn main() {
    // Write
    let mut tmpfile: File = tempfile::tempfile().unwrap();
    write!(tmpfile, "Hello World!").unwrap();

    // Seek to start
    tmpfile.seek(SeekFrom::Start(0)).unwrap();

    // Read
    let mut buf = String::new();
    tmpfile.read_to_string(&mut buf).unwrap();
    assert_eq!("Hello World!", buf);
}

Dependencies

~1.7–7.5MB
~161K SLoC