Skip to content

Instantly share code, notes, and snippets.

@Mic92
Created December 24, 2017 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/30d56aaffe50c279e6f809142929377b to your computer and use it in GitHub Desktop.
Save Mic92/30d56aaffe50c279e6f809142929377b to your computer and use it in GitHub Desktop.
emulate mkdir -p in Rust (create directories recursivly and ignore an existing directory)
use std::io;
use std::path::Path;
use std::fs::create_dir_all;
fn mkdir_p<P: AsRef<Path>>(path: &P) -> io::Result<()> {
if let Err(e) = create_dir_all(path) {
if e.kind() != io::ErrorKind::AlreadyExists {
return Err(e)
}
}
Ok(())
}
@VergeDX
Copy link

VergeDX commented Nov 8, 2022

Update: use fs::create_dir_all instead.
When path is a exists file, the AlreadyExists will be ignored.

@Mic92
Copy link
Author

Mic92 commented Nov 9, 2022

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment