Use Path for immutable references and PathBuf for owned, mutable file paths in Rust.
Use Path for immutable path references and PathBuf for owned, mutable paths. Convert between them using as_path() on PathBuf or Path::new() on string slices.
use std::path::{Path, PathBuf};
fn main() {
let path_buf = PathBuf::from("/home/user/docs");
let path: &Path = path_buf.as_path();
let new_path = Path::new("/tmp");
println!("Owned: {:?}, Borrowed: {:?}", path_buf, new_path);
}
Think of Path as a read-only view of a file location and PathBuf as a container you can edit and own. You use Path when you just need to check if a file exists or read it, and PathBuf when you need to build a path by adding folders or saving it for later. It is like the difference between looking at a map versus holding a physical piece of paper you can write on.