Create temporary directory



examples/files/tempdir-demo/Cargo.toml
[package]
name = "tempdir-demo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tempdir = "0.3"

examples/files/tempdir-demo/src/main.rs
use std::io::{self, Write};
use std::fs::File;
use tempdir::TempDir;


fn main() {
    match write_temp_folder_with_files() {
        Ok(()) => println!("ok"),
        Err(err) => println!("Error {err}"),
    }
}

fn write_temp_folder_with_files() -> io::Result<()> {
    let temp_dir = TempDir::new("demo")?;
    let file_path = temp_dir.path().join("foo.txt");
    println!("{:?}", file_path);

    let mut fh = File::create(file_path)?;
    fh.write_all(b"Hello, world!")?;
    fh.sync_all()?;
    temp_dir.close()?;

    Ok(())
}