use std::env;
use std::path::PathBuf;
fn main() {
let mut proj_dir = PathBuf::from("/");
let cargo_location = env::var("CARGO_MANIFEST_DIR");
if cargo_location.is_ok(){
proj_dir = match cargo_location {
Ok(s) => PathBuf::from(&s),
Err(e) => panic!("{e}"),
};
}
proj_dir.push("src"); // get the src path for example
println!("{proj_dir:#?}");
}
I've been figuring out how to get the current project dir and this is one of the solutions i arrived at
why i need this is simply, i work with files a lot and i want a way to get locations within the project without hardcoding it into the file, so everything is relative.
so say, from this path proj_dir i can do .push("src") and get the path to the src folder, etc.
is there a better, more robust way of doing it?