本文簡要介紹rust語言中 std::process::Child.try_wait
的用法。
用法
pub fn try_wait(&mut self) -> Result<Option<ExitStatus>>
如果孩子已經退出,則嘗試收集孩子的退出狀態。
該函數不會阻塞調用線程,隻會檢查子進程是否退出。如果孩子已經退出,那麽在 Unix 上,進程 ID 將被收割。隻要孩子已經退出,這個函數就保證重複返回一個成功的退出狀態。
如果孩子已經退出,則返回Ok(Some(status))
。如果此時退出狀態不可用,則返回Ok(None)
。如果發生錯誤,則返回該錯誤。
請注意,與 wait
不同,此函數不會嘗試刪除標準輸入。
例子
基本用法:
use std::process::Command;
let mut child = Command::new("ls").spawn().unwrap();
match child.try_wait() {
Ok(Some(status)) => println!("exited with: {}", status),
Ok(None) => {
println!("status not ready yet, let's really wait");
let res = child.wait();
println!("result: {:?}", res);
}
Err(e) => println!("error attempting to wait: {}", e),
}
相關用法
- Rust Child.wait用法及代碼示例
- Rust Child.wait_with_output用法及代碼示例
- Rust Child.kill用法及代碼示例
- Rust Child.id用法及代碼示例
- Rust Child用法及代碼示例
- Rust Chars.as_str用法及代碼示例
- Rust Chain.get_ref用法及代碼示例
- Rust Chain.get_mut用法及代碼示例
- Rust ChunksExact用法及代碼示例
- Rust Chain用法及代碼示例
- Rust CharIndices.offset用法及代碼示例
- Rust Chunks用法及代碼示例
- Rust ChunksMut用法及代碼示例
- Rust ChunksExactMut用法及代碼示例
- Rust Chain.into_inner用法及代碼示例
- Rust CStr.into_c_string用法及代碼示例
- Rust Cursor.new用法及代碼示例
- Rust Condvar.notify_all用法及代碼示例
- Rust Cursor.remaining_slice用法及代碼示例
- Rust Command.args用法及代碼示例
- Rust Condvar.wait用法及代碼示例
- Rust CString.into_bytes用法及代碼示例
- Rust CStr.from_bytes_with_nul用法及代碼示例
- Rust Cow.is_owned用法及代碼示例
- Rust Cow用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::process::Child.try_wait。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。