• 等待 Wait

    等待 Wait

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

    1. use std::process::Command;
    2. fn main() {
    3. let mut child = Command::new("sleep").arg("5").spawn().unwrap();
    4. let _result = child.wait().unwrap();
    5. println!("reached end of main");
    6. }
    1. $ rustc wait.rs && ./wait
    2. reached end of main
    3. # `wait` keeps running for 5 seconds
    4. # `sleep 5` command ends, and then our `wait` program finishes