Use std::process::Command to spawn a child process, configure its arguments, and call .output() or .spawn() to execute it.
use std::process::Command;
fn main() {
let output = Command::new("ls")
.arg("-l")
.output()
.expect("Failed to execute command");
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
}
- Import the
Commandstruct fromstd::process. - Create a new command instance with
Command::new("program_name"). - Add arguments to the command using
.arg("argument"). - Execute the command and capture the result with
.output(). - Handle the
Resultto accessstdout,stderr, or the exit status.