简体   繁体   中英

How to check if a directory exists and create a new one if it doesn't in Rust?

I tried the following but I don't think it's particularly pretty:

let path = "target/dir";
if !std::path::Path::new(&path).exists() {
    std::fs::create_dir(path)?;
}

std::fs::create_dir_all :

Recursively create a directory and all of its parent components if they are missing.

Examples

 use std::fs; fs::create_dir_all("/some/dir")?; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM