子进程

process::Output 结构体表示已结束的子进程(child process)的输出,而 process::Command 结构体是一个进程创建者(process builder)。

  1. use std::process::Command;
  2. fn main() {
  3. let output = Command::new("rustc")
  4. .arg("--version")
  5. .output().unwrap_or_else(|e| {
  6. panic!("failed to execute process: {}", e)
  7. });
  8. if output.status.success() {
  9. let s = String::from_utf8_lossy(&output.stdout);
  10. print!("rustc succeeded and stdout was:\n{}", s);
  11. } else {
  12. let s = String::from_utf8_lossy(&output.stderr);
  13. print!("rustc failed and stderr was:\n{}", s);
  14. }
  15. }

(再试试上面的例子,给 rustc 命令传入一个错误的 flag)