I'm using getopts, and I was previously getting a value from a flag like this:
let file = matches.opt_str("f").unwrap();
let file_path = Path::new(&file);
But, I would like to handle the possible errors better, by making the path optional. This is my new code:
let file = matches.opt_str("f");
let file_path = match file {
Some(f) => Some(Path::new(&f)),
None => None
}
But, when I try to compile this code, I get the error 'f' does not live long enough
. I'm completely stumped.
Heres the MCVE of my code:
fn main() {
let foo = Some("success".to_string());
let string = match foo {
Some(s) => Some(Path::new(&s)),
None => None
};
}