expression

Spawn and reap a child process

A process has its own execution and resource lifetime. Spawning returns a handle before completion; wait obtains the termination status and reaps the child on Unix. Dropping Child does not stop or wait for the process. This fixture launches only a freshly compiled copy of itself with a fixed worker argument; no shell is involved. Expected stdout (not executed): success: true Common mistake: Assuming dropping Child reaps it. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
Command::spawn() -> io::Result<Child>; child.wait()

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 = child.wait()?;    println!("success: {}", status.success());    Ok(())}
Rust Reference