当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust Child.wait_with_output用法及代码示例


本文简要介绍rust语言中 std::process::Child.wait_with_output 的用法。

用法

pub fn wait_with_output(self) -> Result<Output>

同时等待子进程退出并收集 stdout/stderr 句柄上的所有剩余输出,返回 Output 实例。

子进程的 stdin 句柄(如果有)将在等待之前关闭。这有助于避免死锁:它确保子级在父级等待子级退出时不会阻塞等待父级的输入。

默认情况下,标准输入、标准输出和标准错误是从父级继承的。为了将输出捕获到此Result<Output> 中,有必要在父子节点之间创建新管道。分别使用 stdout(Stdio::piped())stderr(Stdio::piped())

例子

use std::process::{Command, Stdio};

let child = Command::new("/bin/cat")
    .arg("file.txt")
    .stdout(Stdio::piped())
    .spawn()
    .expect("failed to execute child");

let output = child
    .wait_with_output()
    .expect("failed to wait on child");

assert!(output.status.success());

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 std::process::Child.wait_with_output。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。