Hello, rustacians!
I am new to Rust. I have a question about functions and returning ownership.
I would like to return ownership of a std::process::Command
variable but I am not able to do it. Rust compiler complains
fn templar_cmd_with_conf(conf: &str) -> Command {
let tmp_dir = TempDir::new().expect("temp_dir failed");
let tmp_dir = tmp_dir.path().to_str().unwrap();
let conf_file: PathBuf = [tmp_dir, ".templar.toml" ].iter().collect();
std::fs::write(conf_file, conf).unwrap();
return Command::cargo_bin("templar").unwrap().arg("--home").arg(tmp_dir);
}
The compilation error I get is:
Compiling templar v0.1.0 (C:\Users\Kostas\dev\workspace\templar)
error[E0308]: mismatched types
--> tests\templar.rs:302:12
|
296 | fn templar_cmd_with_conf(conf: &str) -> Command {
| ------- expected `std::process::Command` because of
return type
...
302 | return Command::cargo_bin("templar").unwrap().arg("--home").arg(tmp_dir);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
expected struct `std::process::Command`, found `&mut std::process::Command`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
Clearly the arg
function returns a mutable reference (Command in std::process - Rust).
Do you know how I convert the mutable reference to an owned variable?
Thank you in advance,
Kostas