我有一个函数,它接收一些参数,但当我尝试使用这些参数时,它会抛出一个错误。
// 这是函数:
// 这是函数:
pub fn solc_compile(compiler: &str, file: &str, out: &str, config: templates::Config) {
let mut args = vec![
"--bin",
"--abi",
"--include-path",
"./libs",
"--include-path",
"./node_modules",
"--output-dir",
out,
];
if config.compiler.optimize {
let runs: &str = config.compiler.runs.to_string().as_str();
args.push("--optimize");
args.push("--optimize-runs");
args.push(runs);
}
}
// 在函数参数中使用的配置类型(config templates::Config)。
templates.rs
// config templates.
#[derive(Deserialize, Serialize)]
pub struct Config {
pub info: ConfigInfo,
pub compiler: ConfigCompiler,
}
// config.info templates.
#[derive(Deserialize, Serialize)]
pub struct ConfigInfo {
pub name: String,
pub license: String,
}
// config.compiler templates.
#[derive(Deserialize, Serialize)]
pub struct ConfigCompiler {
pub solc: String,
pub optimize: bool,
pub runs: i64,
}
这是当我运行cargo build时遇到的错误。
error[E0716]: temporary value dropped while borrowed
--> src/solc.rs:58:26
|
58 | let runs: &str = config.compiler.runs.to_string().as_str();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
...
61 | args.push(runs);
| ---- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
config.compiler.runs.to_string()
获取的值未分配给变量,因此它会过早地被丢弃。请参见:https://dev59.com/Dl4b5IYBdhLWcg3w-14I https://dev59.com/K5ffa4cB1Zd3GeqP3R4s https://dev59.com/CFYN5IYBdhLWcg3wR2k- - E net4error[E0716]
error E0716: temporary value dropped while borrowed (rust),它链接回到这个问题。 - JamesThomasMoon