Use tempfile::tempdir() to create a temporary directory that automatically deletes itself when dropped, or tempfile::NamedTempFile for a single file. In the rustc_codegen_gcc project, tempdir() is used in src/back/lto.rs to store intermediate LTO bitcode files safely during compilation.
use tempfile::tempdir;
fn prepare_lto() -> tempfile::TempDir {
let tmp_path = tempdir().expect("Cannot create temporary directory");
// Use tmp_path.path() to write files; they auto-delete when tmp_path drops
tmp_path
}