Tag Archives: home directory

Rust – get user’s home directory

fn main() {
  let mut path: PathBuf = get_app_dir();
  path.push(".config");
  path.set_file_name("properties");
  path.set_extension("ini");
  let str_path = path.to_str().unwrap();
  if str_path.len() != 0 && path_exists(&path) {
    println!("Path {} exists", str_path);
  }
}
 
fn get_app_dir() -> PathBuf {
    let dir: PathBuf = match env::home_dir() {
        Some(path) => PathBuf::from(path),
        None => PathBuf::from(""),
    };
    dir
}
 
pub fn path_exists(path: &PathBuf) -> bool {
    return Path::new(&path).exists();
}