我不知道如何按照编译器的建议进行操作:考虑使用let绑定来创建寿命更长的值
。
#![allow(unused)]
fn main() {
let a_dir=std::env::args().nth(2);
let dir:&str=match a_dir{
Some (ref a)=> &format!("./{}/**/*.liquid",a) as &str,
None=>{"./**/*.liquid"},
};
}
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:6:23
|
5 | let dir:&str=match a_dir{
| --- borrow later stored here
6 | Some (ref a)=> &format!("./{}/**/*.liquid",a) as &str,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
|
= note: consider using a `let` binding to create a longer lived value
let string
;这样string
就不需要是可变的,编译器足够聪明,可以确定它在所有使用它的代码路径中都被正确初始化。 - user4815162342string
的值会在每次迭代中改变呢? - theberziformat!
。 (https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2d76eee0e2cc109752264bedd178f92c) - theberzi