【发布时间】:2015-12-05 20:30:24
【问题描述】:
我正在使用 getopts,我之前从这样的标志中获取值:
let file = matches.opt_str("f").unwrap();
let file_path = Path::new(&file);
但是,我希望通过将路径设为可选来更好地处理可能出现的错误。这是我的新代码:
let file = matches.opt_str("f");
let file_path = match file {
Some(f) => Some(Path::new(&f)),
None => None
}
但是,当我尝试编译此代码时,我收到错误 'f' does not live long enough。我完全被难住了。
这是我的代码的 MCVE:
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
【问题讨论】: