expression
Close the child input pipe before waiting
A pipe reader observes EOF after all write ends close. Take the owned ChildStdin, send a small fixed payload and drop it before waiting. wait_with_output consumes Child and captures remaining piped streams. For larger bidirectional traffic, use concurrent bounded I/O to avoid pipe-capacity deadlocks. The trusted worker emits output only after reading EOF. Expected stdout (not executed): received: hello Common mistake: Waiting while still owning the writer that the child needs closed. Run only in the isolated fixture runner, not a public code playground.
Formal Syntax & Structure
rust syntaxrust
Stdio::piped(); child.stdin.take(); drop(input); child.wait_with_output()Working Example
Example Snippetrust
use std::{env, io::{self, Read, Write}, process::{Command, Stdio}};fn main() -> io::Result<()> { if env::args().nth(1).as_deref() == Some("worker") { let mut text = String::new(); io::stdin().read_to_string(&mut text)?; println!("received: {}", text); return Ok(()); } let mut child = Command::new(env::current_exe()?).arg("worker") .stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?; let mut input = child.stdin.take().expect("piped stdin"); let written = input.write_all(b"hello"); drop(input); let out = child.wait_with_output()?; written?; assert!(out.status.success()); print!("{}", String::from_utf8_lossy(&out.stdout)); Ok(())}