Rust by Example中文

18.5 Child processes

The ProcessOutput struct represents the output of a finished child process, and the Command struct is a process builder.

use std::process::Command; fn main() { let output = Command::new("rustc") .arg("--version") .output().unwrap_or_else(|e| { panic!("failed to execute process: {}", e) }); if output.status.success() { let s = String::from_utf8_lossy(&output.stdout); print!("rustc succeeded and stdout was:\n{}", s); } else { let s = String::from_utf8_lossy(&output.stderr); print!("rustc failed and stderr was:\n{}", s); } }

(You are encouraged to try the previous example with an incorrect flag passed to rustc)