I have the following code ...
use std::process::Command;
fn main() {
let cmds = vec![vec!["ls", "-lh"], vec!["grep", "foo"]];
let mut processes: Vec<&mut Command> = Vec::new();
let mut i = 0;
let length = cmds.len();
while i < length {
let cmd = cmds[i].clone();
let mut p = Command::new(&cmd[0]).args(&(cmd[1..]));
processes.push(p);
i += 1;
}
println!("processes: {:?}", processes);
// want to manipulate processes elements here again.
// ...
}
Which doesn't compile:
error: borrowed value does not live long enough
--> src/main.rs:11:60
|
11 | let mut p = Command::new(&cmd[0]).args(&(cmd[1..]));
| --------------------- ^ temporary value dropped here while still borrowed
| |
| temporary value created here
...
19 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
I understand why it refused to compile, I just don't know how to fix it in this case.