expression

Poll a child without blocking

try_wait distinguishes an unavailable exit status from a completed process and from an OS error. Scheduling is nondeterministic: either branch may occur here. Fall back to wait so the fixture always obtains a final status. On Unix a completed try_wait also reaps the process. Unlike wait, try_wait does not close child stdin. Expected stdout (not executed): finished: true Common mistake: Using a tight polling loop that consumes CPU or assuming the first poll returns None. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
Child::try_wait() -> io::Result<Option<ExitStatus>>

Working Example

Example Snippetrust
use std::{env, io, process::{Command, Stdio}};fn main() -> io::Result<()> {    if env::args().nth(1).as_deref() == Some("worker") { return Ok(()); }    let mut child = Command::new(env::current_exe()?).arg("worker")        .stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).spawn()?;    let status = match child.try_wait()? { Some(s) => s, None => child.wait()? };    println!("finished: {}", status.success());    Ok(())}
Rust Reference