我正在尝试从中提取可选的arg getopts
,并且获取借入的值对于该变量而言不会存在足够长的错误s
。
码:
let cfgFilePath = match matches.opt_str("c") {
Some(s) => Some(Path::new(&s.clone())),
None => None
};
错误:
main.rs:29:36: 29:45 error: borrowed value does not live long enough
main.rs:29 Some(s) => Some(Path::new(&s.clone())),
^~~~~~~~~
main.rs:31:7: 65:2 note: reference must be valid for the block suffix following statement 10 at 31:6...
main.rs:31 };
main.rs:32 let tmpdir = Path::new(&matches.opt_str("t").unwrap_or("/tmp/".to_string()));
main.rs:33 let name = matches.opt_str("n").unwrap_or_else(||{
main.rs:34 print_usage(&program, opts);
main.rs:35 panic!("error: -n NAME required");
main.rs:36 });
...
无论,还是其他我想尝试的方法.clone()
,都会发生这种情况。.to_owned()
.to_str()
因为Path::new(&x)
返回&Path
从中借用其内容的x
。
Some(s) => Some(Path::new(&s.clone())), // Type is Option<&Path>
// reborrow --------------^
您实际要执行的操作是使用PathBuf
(拥有的等效项Path
)。PathBuf
将获得所有权s
而不是借用它。
let cfgFilePath = match matches.opt_str("c") {
Some(s) => Some(PathBuf::from(s)),
None => None
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句