less than 1 minute read

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");
}

Here is the issue with the checking emptiness of PathBuf.

Here is about PathBuf type in Rust.

Tags:

Categories:

Updated:

Leave a comment