Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Create A Directory In Rust (Recursively)

rust
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 start

This is the basic function I use to make directories in Rust. It's like running [TODO: Code shorthand span ] on the command line.

Footnotes And References