Environment and Process Management

std.process

The std::process module provides types and functions for spawning and interacting with child processes, inspecting the current process ID, and terminating execution with explicit exit codes.

+-------------------------------------------------------------------+
|                           std::process                            |
|                                                                   |
|  Current Process:                                                 |
|    id() -> u32          exit(i32) -> !       abort() -> !         |
|    ExitCode (SUCCESS, FAILURE, From<u8>)   Termination trait      |
|                                                                   |
|  Child Process Management:                                        |
|    Command  ---.spawn()--->  Child (stdin, stdout, stderr, wait)  |
|             ---.output()-->  Output (status, stdout, stderr)      |
|             ---.status()-->  ExitStatus (success, code)           |
|                                                                   |
|  I/O Configuration:                                               |
|    Stdio::inherit()     Stdio::piped()       Stdio::null()        |
+-------------------------------------------------------------------+

Key Types and Functions in std::process

Item Kind Description
Command Struct Builder for configuring and launching a child process.
Child Struct Handle to a running or exited child process returned by Command::spawn().
Output Struct Collected status, stdout (Vec<u8>), and stderr (Vec<u8>) from Command::output().
ExitStatus Struct OS exit status of a finished child process (status.success(), status.code()).
Stdio Struct Describes how a child stream (stdin, stdout, stderr) is connected (inherit, piped, null).
ChildStdin / ChildStdout / ChildStderr Structs Pipe handles to a running Child's standard streams.
ExitCode Struct Portable return type from main representing process success or failure (u8).
Termination Trait Implemented by types that can be returned from fn main().
process::id() Function Returns the OS-assigned process identifier (PID) of the current process as u32.
process::exit(code) Function Terminates the current process immediately with code without running stack destructors.
process::abort() Function Terminates the current process abnormally without cleanup.

Inspecting the Current Process ID and Spawning a Child

std::process::id() returns the current process's PID, while Child::id() returns the spawned child process's PID.

use std::io;
use std::process::{self, Command, Stdio};

fn main() -> io::Result<()> {
    let parent_pid = process::id();
    assert!(parent_pid > 0);

    // Spawn a minimal cross-platform command (`rustc --version`)
    let mut child = Command::new("rustc")
        .arg("--version")
        .stdout(Stdio::piped())
        .spawn()?;

    let child_pid = child.id();
    assert_ne!(parent_pid, child_pid);

    let output = child.wait_with_output()?;
    let version_text = String::from_utf8_lossy(&output.stdout);

    println!("Child exited successfully: {}", output.status.success());
    println!("Output starts with 'rustc': {}", version_text.starts_with("rustc"));
    Ok(())
}
Child exited successfully: true
Output starts with 'rustc': true

Important Drop Behavior of Child

Unlike File or MutexGuard, dropping a std::process::Child handle does not terminate or wait for the child process. The child continues running in the background, and on Unix systems, if the parent never calls child.wait() (or child.try_wait()), the terminated child remains in the OS process table as a zombie process until the parent exits. Always call wait(), wait_with_output(), or kill() followed by wait() on every Child you spawn.