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:
use std::path::Path;
fn main() {
let foo = Some("success".to_string());
let string = match foo {
Some(s) => Some(Path::new(&s)),
None => None
};
}
error[E0597]: `s` does not live long enough
--> src/main.rs:6:35
|
5 | let string = match foo {
| ------ borrow later stored here
6 | Some(s) => Some(Path::new(&s)),
| ^^ - `s` dropped here while still borrowed
| |
| borrowed value does not live long enough