(有一百万个问题有类似的标题,但我认为这个问题与所有问题都不同.)
铁 rust 版本:1.69.0.
以下内容的工作方式与预期一致:
fn main() {
println!("{}", format_args!("hello {}", "world"));
}
但borrow 判断器会阻止编译以下代码.
fn main() {
let args = format_args!("hello {}", "world");
println!("{}", args);
}
错误:
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:2:16
|
2 | let args = format_args!("hello {}", "world");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
3 | println!("{}", args);
| ---- borrow later used here
|
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a `let` binding to create a longer lived value
|
2 ~ let binding = format_args!("hello {}", "world");
3 ~ let args = binding;
|
我看不到任何违反Rust借入规则的行为--据我所知,一切都在作用域内,绑定到一个变量,没有过早丢弃等等.fmt::Arguments<'a>
有一个生存期参数,但不清楚它试图保留哪些数据正在被丢弃.此外,帮助信息显然是虚假的(args
的生命周期 与binding
的生命周期 一样长!).