Checking emptiness of PathBuf variable
If you are working with a path of a file in Rust such as manipulating the path of a file by appending, the PathBuf type is useful. I want to compare the emptiness of PathBuf data. Here’s how to do it.
use std::path::PathBuf;
let mut path = PathBuf::new();
let is_empty: bool = path.as_os_str().is_empty();
println!("{:?}", is_empty);
# true
if is_empty {
path.push("empty");
} else {
path.push("not_empty");
}
Leave a comment