Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

error[E0716]: temporary value dropped while borrowed

I have a function which receive some parameter, but when I try to use the parameter, it throw an error.

// the function

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 type using on the function parameter (config templates::Config).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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,
}

And this is the error I’ve got when run 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

>Solution :

You create a new String with to_string, but don’t give it a variable to live in, so it dies at the next ;. You can solve that by not calling as_ref immediately:

if config.compiler.optimize {
    let runs: &str = config.compiler.runs.to_string();
    // ..
    args.push(runs.as_ref());
}

But now you have the next problem: your args vector contains string references, but the string in runs dies at the end of the if block. It must be able to live at least as long as args. You can solve this by splitting declaration and assignment for args:

let runs: String;
if config.compiler.optimize {
    runs = config.compiler.runs.to_string();
    // ...
    args.push(runs.as_str());
}

Another possibility would be to make args a Vec<String>.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading