logo
pub fn tempfile() -> Result<File>
Expand description

Create a new temporary file.

The file will be created in the location returned by std::env::temp_dir().

Security

This variant is secure/reliable in the presence of a pathological temporary file cleaner.

Resource Leaking

The temporary file will be automatically removed by the OS when the last handle to it is closed. This doesn’t rely on Rust destructors being run, so will (almost) never fail to clean up the temporary file.

Errors

If the file can not be created, Err is returned.

Examples

use tempfile::tempfile;
use std::io::{self, Write};

// Create a file inside of `std::env::temp_dir()`.
let mut file = tempfile()?;

writeln!(file, "Brian was here. Briefly.")?;