02-等待

等待 — Rust By Example

译文 · 基于 Rust By Example

原文链接: https://doc.rust-lang.org/stable/rust-by-example/std_misc/process/wait.html

等待

如果你想等待一个 process::Child 完成,就必须调用 Child::wait,这会返回一个 process::ExitStatus。

1
2
3
4
5
6
7
8
use std::process::Command;

fn main() {
    let mut child = Command::new("sleep").arg("5").spawn().unwrap();
    let _result = child.wait().unwrap();

    println!("reached end of main");
}
1
2
3
4
$ rustc wait.rs && ./wait
reached end of main
# `wait` keeps running for 5 seconds
# `sleep 5` command ends, and then our `wait` program finishes
最后修改 August 21, 2026: 更新 (76fc81a2e)