How to get only the directory portion of the current executable's path in Rust?
By usingPathBuf::push
This type provides methods like push and set_extension that mutate the path in place. It also implements Deref to Path, meaning that all methods on Path slices are available on PathBuf values as well.Example code
use std::env;
use std::io;
use std::path::PathBuf;
fn inner_main() -> io::Result<PathBuf> {
let mut dir = env::current_exe()?;
dir.pop();
dir.push("subfolder_name");
dir.push("file_inside_subfolder.txt");
Ok(dir)
}
fn main() {
let path = inner_main().expect("Couldn't");
println!("The path is: {}", path.display());
}