home
NOTE: Under Construction - I'm in the middle of upgrading my site and lots of stuff is kinda broken. Please forgive the mess.

Create A Directory In Rust (Recursively)

June 2023

This is the basic function I use to make directories in Rust. It's like running mkdir -p /some/path on the command line.

Code
use std::path::PathBuf;

fn main() {
    let dir = PathBuf::from("some/path");
    match mkdir_p(dir) {
        Ok(_) => println!("Directory was made or already exists"),
        Err(e) => println!("Error: {}", e)
    }
}

fn mkdir_p(dir: PathBuf) -> std::io::Result<()> {
    if dir.exists() {
        Ok(())
    } else {
        std::fs::create_dir_all(dir)
    }
}
Results
Directory was made or already exists
═══ § ═══

Footnotes And References