expression

Capture stdout and stderr separately

output waits and captures both byte streams. A nonzero child exit is not itself an I/O error: examine status separately. The worker emits only small fixed strings. Capturing untrusted unlimited output in memory is inappropriate; implement size limits and a deadline for real workloads. Expected stdout (not executed): code: Some(7) stdout: report stderr: invalid record Common mistake: Treating a successful output() call as proof of successful worker execution. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
Command::output() -> io::Result<Output>

Working Example

Example Snippetrust
use std::{env, io, process::{self, Command}};fn main() -> io::Result<()> {    if env::args().nth(1).as_deref() == Some("worker") {        println!("report"); eprintln!("invalid record"); process::exit(7);    }    let out = Command::new(env::current_exe()?).arg("worker").output()?;    println!("code: {:?}", out.status.code());    println!("stdout: {}", String::from_utf8_lossy(&out.stdout).trim_end());    println!("stderr: {}", String::from_utf8_lossy(&out.stderr).trim_end());    Ok(())}
Rust Reference