std-function

fs::remove_dir: remove an empty directory

Remove an empty directory. Unlike recursive removal, this intentionally fails if entries remain. Use a fresh disposable working directory exclusively controlled by this program. Expected stdout (not executed): false Common mistake: Expecting nonempty directories to disappear.

Formal Syntax & Structure

rust syntaxrust
fs::remove_dir(path) -> io::Result<()>

Working Example

Example Snippetrust
fn main() {    use std::fs::{self, File};    use std::io::{Read, Write};    fs::create_dir("bm-fixture").expect("fresh working directory required");    let p = std::path::Path::new("bm-fixture").join("note.txt");    let d = p.with_file_name("empty");    fs::create_dir(&d).unwrap();    fs::remove_dir(&d).unwrap();    println!("{}", d.try_exists().unwrap());}
Rust Reference