Process boundaries
Quick answer
A child process executes separately from its parent. Command starts a program with explicit arguments; its exit status and output describe the result. A successful launch does not prove the child completed its task successfully.
Arguments and lifecycle
arg passes a single argument; it does not run a shell by default. Use a trusted executable and avoid shell interpolation. output waits and captures stdout and stderr in memory; check status.success(). For long-running or untrusted workloads, bound execution time and output, and reap children. Dropping Child alone does not wait for it.
use std::process::Command;fn main() { let mut command = Command::new("example-worker"); command.arg("two words"); let args: Vec<_> = command.get_args().collect(); println!("arguments: {}, value: {}", args.len(), args[0].to_str().unwrap()); }Expected stdout (not executed):
arguments: 1, value: two wordsNo executable is launched; the value stays one argument.
use std::{env, io, process::Command};fn main() -> io::Result<()> { if env::args().nth(1).as_deref() == Some("worker") { println!("ready"); return Ok(()); } let result = Command::new(env::current_exe()?).arg("worker").output()?; println!("success: {}", result.status.success()); print!("{}", String::from_utf8_lossy(&result.stdout)); Ok(())}Manual local Rust/Cargo candidate; not executed. Expected behavior:
success: truereadyManual native lab only, no extra dependency: compile the trusted code locally and run with an external timeout in an isolated environment; it starts itself once.
Common mistake
Do not equate Ok(Output) with a successful child exit code.
Try it yourself
Explain why capturing stdout from an unbounded worker could exhaust memory.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Process boundaries — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A child program needs the literal argument `a b`. How should you pass it?
Process boundaries — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::process::Command; fn main() { let mut command = Command::new("example-worker"); command.arg("two words"); let args: Vec<_> = command.get_args().collect(); println!("arguments: {}, value: {}", args.len(), args[0].to_str().unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Process boundaries — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{env, io, process::Command}; fn main() -> io::Result<()> { if env::args().nth(1).as_deref() == Some("worker") { println!("ready"); return Ok(()); } let result = Command::new(env::current_exe()?).arg("worker").output()?; println!("success: {}", result.status.success()); print!("{}", String::from_utf8_lossy(&result.stdout)); Ok(()) } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.