47 stable releases (3 major)

3.20.0 May 11, 2025
3.19.1 Mar 19, 2025
3.17.1 Feb 17, 2025
3.14.0 Nov 8, 2024
0.5.1 May 22, 2015

#1 in Filesystem

Download history 3258194/week @ 2025-02-21 3328242/week @ 2025-02-28 3672416/week @ 2025-03-07 3757900/week @ 2025-03-14 4236099/week @ 2025-03-21 3416845/week @ 2025-03-28 3597346/week @ 2025-04-04 3355165/week @ 2025-04-11 3131218/week @ 2025-04-18 3215514/week @ 2025-04-25 3129355/week @ 2025-05-02 3327106/week @ 2025-05-09 3547299/week @ 2025-05-16 3031580/week @ 2025-05-23 3131457/week @ 2025-05-30 2659641/week @ 2025-06-06

12,951,505 downloads per month
Used in 20,135 crates (6,909 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

~2–12MB
~156K SLoC