Rust Reference ยท Standard Library Modules
The std::process module
Reference for std::process: run programs with Command, capture Output, spawn a Child, pipe stdin and stdout, ExitStatus, process::exit and ExitCode.
std::process lets a program start other programs and control how it ends itself. Command is a builder for launching an external program; running it produces an Output (everything the program printed plus its exit status) or a Child handle you can talk to while it runs. For the current process there are exit, abort, id and the ExitCode type that main can return.
Spawning a process is not the same as running a shell. Command::new("ls").arg("-l") runs ls directly with one argument; no quoting rules or wildcards are interpreted unless you explicitly start a shell.
use std::process::Command; let out = Command::new("echo").arg("hi").output()?;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
| Item | Description |
|---|---|
Command::new(program) |
Builder. Methods: arg, args, env, env_clear, current_dir, stdin, stdout, stderr. |
.output() |
Run to completion, capture stdout and stderr: io::Result<Output>. |
.status() |
Run to completion with inherited stdio: io::Result<ExitStatus>. |
.spawn() |
Start without waiting: io::Result<Child>. |
Child |
Fields stdin, stdout, stderr (Option<...>); methods wait, try_wait, wait_with_output, kill, id. |
Stdio |
piped(), inherit(), null(); also built from a File. |
ExitStatus |
success(), code() -> Option<i32>; on Unix signal() through ExitStatusExt. |
Output |
Struct with status, stdout: Vec<u8>, stderr: Vec<u8>. |
exit(code: i32) -> ! |
End the process immediately. |
abort() -> ! |
End abnormally (SIGABRT on Unix). |
id() -> u32 |
Current process id. |
ExitCode |
SUCCESS, FAILURE, from(u8); returned from main (see Termination). |
Errors and edge cases: all launch functions return io::Result; NotFound or PermissionDenied mean the program never started. A started program that fails gives Ok with a non-zero status. code() is None when a Unix process was killed by a signal.
Usage notes
process::exitskips destructors of the current stack, so bufferedBufWriters and temp files are not flushed or cleaned. Prefer returningExitCodefrommain.- A
Childthat is dropped is not killed and not waited for; callwaitto avoid zombie processes. - If you pipe both stdin and stdout, write from a separate thread or use
wait_with_outputafter closing stdin, or large data can deadlock. - Arguments are passed verbatim with no shell parsing. Never build a shell command line from untrusted text (
sh -c); pass arguments separately. - Programs are looked up through the
PATHof the parent unless you give a path. - Exit codes are 0 to 255 on Unix; other values are truncated by the operating system.
- Only a sandbox-local set of tools exists on the browser runner (
echo,sort,sh); the examples rely on those.