Expand description
Temporary files and directories with automatic cleanup.
- Crate
::tempfile
. - docs.rs
- crates.io
- GitHub
tempfile
provides secure, cross-platform temporary file and directory management
with automatic cleanup when the handles are dropped.
Managing temporary files correctly on many platforms involves a lot of technical tradeoffs and there are many ways to do it poorly. This crate is ancient and battle tested and well documented.
§Examples
Creating and using a temporary directory:
use tempfile::TempDir;
use std::fs::File;
use std::io::Write;
let dir = TempDir::with_prefix("myapp-tests")?;
// Create files inside the temporary directory
let file_path = dir.path().join("test.txt");
let mut file = File::create(&file_path)?;
writeln!(file, "temporary data")?;
assert!(file_path.exists());
// Directory and all contents are deleted when dropped
drop(dir);
assert!(!file_path.exists());
Modules§
Structs§
- Builder
- Create a new temporary file or directory with custom options.
- Named
Temp File - A named temporary file.
- Path
Persist Error - Error returned when persisting a temporary file path fails.
- Persist
Error - Error returned when persisting a temporary file fails.
- Spooled
Temp File - An object that behaves like a regular temporary file, but keeps data in memory until it reaches a configured size, at which point the data is written to a temporary file on disk, and further operations use the file on disk.
- TempDir
- A directory in the filesystem that is automatically deleted when it goes out of scope.
- Temp
Path - A path to a named temporary file without an open file handle.
Enums§
- Spooled
Data - A wrapper for the two states of a
SpooledTempFile
. Either:
Functions§
- spooled_
tempfile - Create a new
SpooledTempFile
. Also seespooled_tempfile_in
. - spooled_
tempfile_ in - Construct a new
SpooledTempFile
, backed by a file in the specified directory. Use this when, e.g., you need the temporary file to be backed by a specific filesystem (e.g., when your default temporary directory is in-memory). Also seespooled_tempfile
. - tempdir
- Create a new temporary directory. Also see
tempdir_in
. - tempdir_
in - Create a new temporary directory in a specific directory. Also see
tempdir
. - tempfile
- Create a new temporary file. Also see
tempfile_in
. - tempfile_
in - Create a new temporary file in the specified directory. Also see
tempfile
.