當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。