intermediateFile I/O
Directory Operations
Create, read, and manage directories.
Directory Operations
Create, read, and manage directories.
Difficulty
Intermediate
Code
rust
use std::fs;
use std::io;
use std::path::Path;
fn main() -> io::Result<()> {
fs::create_dir_all("test_dir/sub")?;
fs::write("test_dir/a.txt", "hello")?;
fs::write("test_dir/b.txt", "world")?;
for entry in fs::read_dir("test_dir")? {
let entry = entry?;
println!(" {}", entry.path().display());
}
fs::remove_dir_all("test_dir")?;
Ok(())
}Explanation
fs::create_dir_all, read_dir, remove_dir_all for directories.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the file-io category to build a complete understanding of this topic.