Execute system commands in Rust using std::process::Command to spawn processes and capture their output.
Use std::process::Command to spawn external processes, configure arguments, and capture output.
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));
}
Executing system commands from Rust lets your program run other programs on your computer, like a shell command. It is useful when you need to automate tasks or interact with existing tools. Think of it as your program asking the operating system to run a separate task and waiting for the result.