rust create directory if not exists
You can use the std::fs::create_dir
function from the Rust standard library to create a directory if it doesn't already exist. Here's an example:
use std::fs;
fn main() {
let dir_path = "path/to/directory";
if let Err(e) = fs::create_dir(dir_path) {
if e.kind() != std::io::ErrorKind::AlreadyExists {
panic!("Failed to create directory: {}", e);
}
}
}
This code snippet uses the create_dir
function to create a directory at the specified path. If the directory already exists, the function will return an error of type std::io::ErrorKind::AlreadyExists
. In this case, we check the error kind and ignore it, as we only want to create the directory if it doesn't already exist.