std-type
NamedTempFile: owned temporary file
Create a uniquely named temporary file with managed cleanup. Close explicitly to handle cleanup errors; do not assume the name remains valid forever. Expected stdout (not executed): draft Common mistake: Assuming automatic cleanup is guaranteed after abrupt process termination.
Formal Syntax & Structure
rust syntaxrust
tempfile::NamedTempFile::new() -> io::Result<NamedTempFile>Working Example
Example Snippetrust
fn main() { use std::io::{Read, Seek, SeekFrom, Write}; let mut f = tempfile::NamedTempFile::new().unwrap(); f.write_all(b"draft").unwrap(); f.seek(SeekFrom::Start(0)).unwrap(); let mut text = String::new(); f.read_to_string(&mut text).unwrap(); println!("{text}"); f.close().unwrap();}