Use PathBuf for mutable paths and Path for immutable references in Rust file system operations.
Use PathBuf for mutable file paths and Path for immutable references; convert between them using .as_path() or Path::new().
use std::path::{Path, PathBuf};
fn main() {
let mut path = PathBuf::from("/home/user/docs");
path.push("file.txt");
let path_ref: &Path = path.as_path();
println!("Path: {:?}", path_ref);
}
Think of PathBuf as a writable address book entry you can change, while Path is a read-only view of that address. You use PathBuf when building or modifying file locations, and Path when passing them to functions that just need to read the location. It's like having a sticky note you can rewrite versus a printed label you can only look at.