Creates a temporary which is freed while still in use Again :)


i have thinking i have i have seen through the work of the owernship,borrowing and lifetime system from rust.


pub fn play(adress: &str) {
    let location = &ini["mpv"]["location"];
    let location_test = r"c:\PortableApps\play.exe";
    let process = Command::new(mpv_location_test)
	    .arg (&adress);
	    // .output();

still output:

error[E0716]: temporary value dropped while borrowed
  --> src\
24 |     let process = Command::new(location_test)
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
25 |         .arg (&adress);
   |                       - temporary value is freed at the end of this statement
26 |         // .output();
27 |     process.spawn();
   |     ------- borrow later used here
   = note: consider using a `let` binding to create a longer lived value

Okay when i call the command in a onliner all is good:

let process = Command::new(mpv_location_test).arg(&adress).output();

And i understand that rust don’t can hold the variable-value after the let process and its gone when i process.output or spawn after the creation of process.

Hmm whats going wrong? Is it why i borrow the variable that i move to location and why location_test, its an cool String or i think an str?


Edit: arrrrrgs he mean the &adress ??? But why ???
Edit: Ahhh he means the variable process, or??? Why process is freed after use??? But this cannot be when i leave the arg-method and call after the spawn all functioned, it must be the adress-variable???

1 Like

Variables are semantically meaningful in Rust. Expression assigned to a variable lives longer than the same expression used as part of another expression.

Because Rust doesn’t have garbage collection, this:

let process = Command::new(location_test).arg(address);

is different from this:

let process = Command::new(location_test);

You need to use the second version to own the Command instead of only being able to have a temporary reference to it that arg returns.


@kornel Cool ist functioned it’s an if (oneliner) or else (all methods with the associated variable), i understand :slight_smile:.

“I want to love rust, but love is not an computer code!” :slight_smile: :slight_smile: :slight_smile:

Rust are rigth, every time! I should accept the wisdom of rust. :sunny: