So, now and then i am looking into Rust, and this time i have the simple task to get the path to my bash history file. So, you come up with env::var() and env::home_dir() and like to join them. Now, it's like 1 or 2 lines in python and probably in C, i came up with this hideous 3 liner:
let h_file = env::var("HISTFILE").unwrap_or(OsString::from_string(".bash_history".to_string())).into_string().unwrap_or_else(|_| { panic!("the end is near!!!")}); let h_dir = env::home_dir().unwrap_or_else(|| { panic!("unable to get homedir!") } ); let h_file_p = h_dir.join(h_file);
What would be the better way? To be honest, i am concerned that, as a beginner, just using the docs, i came up with is this hideous thing.
Edit: Of course the point is that the first line is that long, and i am aware that i could put all those commands in several lines following each other or use a gazillion match statements, all of which would not really make this a nice solution for a basic task..
I think you are suffering because of the transition between
std::old_path
andstd::path
, namely on the return value ofhome_dir()
. Once it returns astd::path::PathBuf
, it will look like:The stable version of this is: