#temporary-files

tempfile

A library for managing temporary files and directories

46 stable releases (3 major)

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

#1 in Filesystem

Download history 2546276/week @ 2024-12-05 2480969/week @ 2024-12-12 1647710/week @ 2024-12-19 1247806/week @ 2024-12-26 2172714/week @ 2025-01-02 2703381/week @ 2025-01-09 2569444/week @ 2025-01-16 2648141/week @ 2025-01-23 2885089/week @ 2025-01-30 3115820/week @ 2025-02-06 2938997/week @ 2025-02-13 3334566/week @ 2025-02-20 3203614/week @ 2025-02-27 3694881/week @ 2025-03-06 3712841/week @ 2025-03-13 3661711/week @ 2025-03-20

14,848,978 downloads per month
Used in 18,652 crates (6,193 directly)

MIT/Apache

105KB
1K 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
~150K SLoC