05-子进程
子进程 — Rust By Example
译文 · 基于 Rust By Example
原文链接: https://doc.rust-lang.org/stable/rust-by-example/std_misc/process.html
子进程
process::Output 结构体表示已结束的子进程(child process)的输出,而
process::Command 结构体是一个进程创建者(process builder)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 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);
}
}
|
(再试试上面的例子,给 rustc 命令传入一个错误的 flag)