I find Option to be somewhat confusing when it comes to lifetime scoping on variables. I've been trying to extract the underlying value from Some inside of an Option match pattern like so:
let filename = match canonicalize(&PathBuf::from(filename)).unwrap().to_str() {
Some(path) => path,
_ => "",
};
// Later, I validate the string content
if filename.is_empty() {
do_something();
}
However, this throws the following error:
temporary value dropped while borrowed: borrow later used here
The documentation doesn't give much insight on how to persist lifetimes outside of the scope of the call they're initialized in. I have seen examples here and here. Both examples imply either throwing out the value, or converting to an Option, neither of which I want. I simply want to get to the underlying &str value once the function succeeds.