I'm new to Rust, trying to learn safe programming by working with the borrow checker. One thing I tried was to construct a std::process::Command
based on input.
If I just wanted to do what all the examples in the documentation assume I want to do and just run a command with arguments that I know at coding time, it works fine:
use std::process::Command;
fn main() {
let mut command = Command::new("/usr/bin/x-terminal-emulator")
.arg("-e")
.arg("editor")
.output()
.unwrap();
}
I'm trying to run a command that I build at runtime. In order to do this, I need to separate the construction of the Command
struct from the building of its arguments. When I do it this way, the compiler complains about mismatched types:
use std::env::args;
use std::process::Command;
fn main() {
let args = args().collect::<Vec<_>>();
let mut command = Command::new("/usr/bin/x-terminal-emulator");
for arg in &args[1..args.len()] {
command = command.arg(arg);
}
}
The error I get is
mismatched types: expected
std::process::Command
, found&mut std::process::Command
Looking at the documentation for std::process::Command::arg
, it says that it expects a &mut self
and returns a &mut Command
. According to the compiler, that's exactly what it's getting. Is the documentation wrong, or (far more likely) am I misunderstanding something here?