command
import std::process::command; · source
Types
Command
type struct Command {
program: String;
args: Array<String>;
envs: Array<EnvEntry>;
clear_env_flag: boolean;
cwd: Option<String>;
stdin_cfg: Stdio;
stdout_cfg: Stdio;
stderr_cfg: Stdio;
static new(program: Str) -> Command;
arg(mut &this, value: Str) -> void;
env(mut &this, key: Str, value: Str) -> void;
env_clear(mut &this) -> void;
cwd(mut &this, path: Str) -> void;
stdin(mut &this, setting: Stdio) -> void;
stdout(mut &this, setting: Stdio) -> void;
stderr(mut &this, setting: Stdio) -> void;
spawn(&this) -> Result<Child, IoError>;
output(&this) -> Result<Output, IoError>;
status(&this) -> Result<ExitStatus, IoError>;
async collect(&this) -> Result<Output, IoError>;
async run(&this) -> Result<ExitStatus, IoError>;
static shell(command: string) -> i32;
static shell(command: string) -> i32;
}
The builder. Configure it, then finish with spawn, output, or status — or their async spellings, collect and run. For one command line through the platform shell, see exec.
import std::process::command;
mut cmd: Command = Command::new(Str::new("git"));
cmd.arg(Str::new("rev-parse"));
cmd.arg(Str::new("HEAD"));
cmd.stdout(Stdio::Piped);
match (cmd.output()) {
Result::Ok(out) => { /* out.stdout() is a Slice<u8> */ }
Result::Err(e) => { /* IoError */ }
}
new(program) resolves the program against $PATH at spawn time; pass an absolute path to skip the lookup. arg, env, and cwd build up the argv, environment, and working directory; env_clear stops the parent's environment from being inherited, so only env() entries reach the child. stdin, stdout, and stderr each take a Stdio setting.
Properties
| Property | Type |
|---|---|
program | String |
args | Array<String> |
envs | Array<EnvEntry> |
clear_env_flag | boolean |
cwd | Option<String> |
stdin_cfg | Stdio |
stdout_cfg | Stdio |
stderr_cfg | Stdio |
Constructors
static new(program: Str) -> Command;
static new(program: Str) -> Command;Start a new command targeting program. The program name is resolved against $PATH by execvp at spawn time; pass an absolute path to skip the lookup.
Static functions
static shell(command: string) -> i32;
static shell(command: string) -> i32;Run command through the platform shell (/bin/sh -c on POSIX, cmd /c on Windows), inheriting stdio, and return the child's exit code (0 on success), or -1 if the shell could not be spawned or the child was killed by a signal. The cross-platform replacement for the raw system(3) intrinsic when a command genuinely needs shell semantics (pipes, redirects, glob expansion). For a plain program invocation, build the argv with new/arg and call status instead.
static shell(command: string) -> i32;
Methods
arg(mut &this, value: Str) -> void;
env(mut &this, key: Str, value: Str) -> void;
env_clear(mut &this) -> void;
env_clear(mut &this) -> void;Don't inherit the parent's environment. Only the entries added with env() reach the child.
cwd(mut &this, path: Str) -> void;
stdin(mut &this, setting: Stdio) -> void;
stdout(mut &this, setting: Stdio) -> void;
stderr(mut &this, setting: Stdio) -> void;
spawn(&this) -> Result<Child, IoError>;
spawn(&this) -> Result<Child, IoError>;fork + configure fds/env + execvp. Returns a Child the caller must eventually wait on.
output(&this) -> Result<Output, IoError>;
output(&this) -> Result<Output, IoError>;spawn + wait + collect stdout/stderr into arrays. Stdin is left Inherit (or whatever the caller set). Use this for the common "run this and tell me what it printed" case.
status(&this) -> Result<ExitStatus, IoError>;
status(&this) -> Result<ExitStatus, IoError>;spawn + wait. Stdin/out/err go wherever the caller configured (typically Inherit).
async collect(&this) -> Result<Output, IoError>;
async collect(&this) -> Result<Output, IoError>;Spawn, capture both streams, and await the exit without blocking the calling thread: the async spelling of output.
async run(&this) -> Result<ExitStatus, IoError>;
async run(&this) -> Result<ExitStatus, IoError>;Spawn and await the exit without capturing anything: the async spelling of status. Stdio goes wherever the caller configured it.
Finishing
spawn forks, configures, and execs, handing back a Child you must eventually wait on. output spawns, waits, and collects stdout and stderr — the "run this and tell me what it printed" case — while status spawns and waits with the streams going wherever you configured them. collect and run are the same two without blocking the executor: the drains and the exit wait sit on the reactor (Linux) or the blocking pool (Windows). Use collect when anything is Piped, or the child can stall on a full pipe.
Output
type struct Output {
status: ExitStatus;
stdout: Array<u8>;
stderr: Array<u8>;
status(&this) -> ExitStatus;
stdout(&this) -> Slice<u8>;
stderr(&this) -> Slice<u8>;
}
Output exposes status(), stdout(), and stderr(). Both slices borrow the Output and are valid only until it drops — copy out anything you need to keep.
output and collect drain the captured pipes before waiting, which is what avoids the classic deadlock of a child blocked on a full pipe while the parent is blocked in wait. On Windows a captured child occupies two blocking-pool threads while draining, so a program running N children concurrently should size the pool for 2N there.
Properties
| Property | Type |
|---|---|
status | ExitStatus |
stdout | Array<u8> |
stderr | Array<u8> |
Methods
status(&this) -> ExitStatus;
status(&this) -> ExitStatus;The child's decoded exit status.
stdout(&this) -> Slice<u8>;
stdout(&this) -> Slice<u8>;Captured stdout as a borrowed byte slice. Valid until this Output is dropped; copy out anything you need to keep.
stderr(&this) -> Slice<u8>;
stderr(&this) -> Slice<u8>;Captured stderr as a borrowed byte slice. Valid until this Output is dropped.
Stdio
type enum Stdio {
Inherit;
Null;
Piped;
Fd(i32);
}
Inherit shares the parent's fd and is the default; Null redirects to or from /dev/null; Piped creates a pipe whose parent end lands in Child::stdin / stdout / stderr; Fd(i32) uses a specific descriptor, and ownership transfers — the spawn dups it into the child and closes it in the parent.
If
execvpfails in the child, it exits with 127 — the standard "command not found" status — so the parent sees that rather than a hang.
Variants
| Variant | Notes |
|---|---|
Inherit | Share the parent's fd (the default). |
Null | Redirect to / from /dev/null. |
Piped | Create a pipe; the parent keeps its end in the resulting Child::stdin / stdout / stderr. |
Fd(i32) | Use a specific fd. Ownership transfers; the spawn closes the fd in the parent after duping it into the child. |
Trait implementations
Command
implement trait Drop for struct Command
Output
implement trait Drop for struct Output
EnvEntry
implement trait Drop for struct EnvEntry
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Command {
program: String;
args: Array<String>;
envs: Array<EnvEntry>;
clear_env_flag: boolean;
cwd: Option<String>;
stdin_cfg: Stdio;
stdout_cfg: Stdio;
stderr_cfg: Stdio;
}Command: builder for spawning a subprocess.
Command.program: Stringtype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringCommand.args: Array<String>type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arrayCommand.envs: Array<EnvEntry>type struct EnvEntry {
key: String;
value: String;
}Command.clear_env_flag: booleanCommand.cwd: Option<String>Command.stdin_cfg: Stdiotype enum Stdio {
Inherit;
Null;
Piped;
Fd(i32);
}How a child's stdin / stdout / stderr is wired up.
std::process::commandCommand.stdout_cfg: StdioCommand.stderr_cfg: Stdiostatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Command::new(program: Str) -> CommandStart a new command targeting program. The program name is resolved against $PATH by execvp at spawn time; pass an absolute path to skip the lookup.
(parameter) program: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype struct Command {
program: String;
args: Array<String>;
envs: Array<EnvEntry>;
clear_env_flag: boolean;
cwd: Option<String>;
stdin_cfg: Stdio;
stdout_cfg: Stdio;
stderr_cfg: Stdio;
}Command: builder for spawning a subprocess.
std::process::commandCommand::arg(mut &this, value: Str) -> voidmut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) value: StrCommand::env(mut &this, key: Str, value: Str) -> void(parameter) key: StrCommand::env_clear(mut &this) -> voidDon't inherit the parent's environment. Only the entries added with env() reach the child.
(parameter) path: StrCommand::stdin(mut &this, setting: Stdio) -> void(parameter) setting: StdioCommand::stdout(mut &this, setting: Stdio) -> voidCommand::stderr(mut &this, setting: Stdio) -> voidCommand::spawn(&this) -> Result<Child, IoError>fork + configure fds/env + execvp. Returns a Child the caller must eventually wait on.
type struct Child {
stdin: Option<ChildStdin>;
stdout: Option<ChildStdout>;
stderr: Option<ChildStderr>;
}Child: a running subprocess.
std::process::childtype struct IoError {
kind: IoErrorKind;
os_code: i32;
}Command::output(&this) -> Result<Output, IoError>spawn + wait + collect stdout/stderr into arrays. Stdin is left Inherit (or whatever the caller set). Use this for the common "run this and tell me what it printed" case.
type struct Output {
status: ExitStatus;
stdout: Array<u8>;
stderr: Array<u8>;
}Command::status(&this) -> Result<ExitStatus, IoError>spawn + wait. Stdin/out/err go wherever the caller configured (typically Inherit).
type struct ExitStatus {
code: i32;
signal: i32;
signaled: boolean;
}Result of a child's termination. Either it exited normally with a code, or it was killed by a signal. POSIX encodes both into a single wstatus word; ExitStatus is the decoded form.
async function name(param: Type) -> T { ... }Compiles the function into a stackless state machine. Calling it builds a future whose output is the declared return type; nothing runs until that future is polled. Allowed on free functions, methods, and trait methods.
language referenceasync Command::collect(&this) -> Result<Output, IoError>Spawn, capture both streams, and await the exit without blocking the calling thread: the async spelling of output.
async Command::run(&this) -> Result<ExitStatus, IoError>Spawn and await the exit without capturing anything: the async spelling of status. Stdio goes wherever the caller configured it.
static Command::shell(command: string) -> i32Run command through the platform shell (/bin/sh -c on POSIX, cmd /c on Windows), inheriting stdio, and return the child's exit code (0 on success), or -1 if the shell could not be spawned or the child was killed by a signal.
(parameter) command: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut cmd: Commandstatic Command::new(program: Str) -> CommandStart a new command targeting program. The program name is resolved against $PATH by execvp at spawn time; pass an absolute path to skip the lookup.
static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
mut cmd: CommandCommand::arg(mut &this, value: Str) -> voidCommand::stdout(mut &this, setting: Stdio) -> voidStdio::PipedCreate a pipe; the parent keeps its end in the resulting Child::stdin / stdout / stderr.
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
Command::output(&this) -> Result<Output, IoError>spawn + wait + collect stdout/stderr into arrays. Stdin is left Inherit (or whatever the caller set). Use this for the common "run this and tell me what it printed" case.
Result::Ok(T)out: Outputpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)e: IoErrortype struct Output {
status: ExitStatus;
stdout: Array<u8>;
stderr: Array<u8>;
}Output.status: ExitStatusOutput.stdout: Array<u8>Output.stderr: Array<u8>type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::slicetype enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum Stdio {
Inherit;
Null;
Piped;
Fd(i32);
}How a child's stdin / stdout / stderr is wired up.
Stdio::InheritShare the parent's fd (the default).
Stdio::NullRedirect to / from /dev/null.
Stdio::PipedCreate a pipe; the parent keeps its end in the resulting Child::stdin / stdout / stderr.
Stdio::Fd(i32)Use a specific fd. Ownership transfers; the spawn closes the fd in the parent after duping it into the child.
Command::spawn(&this) -> Result<Child, IoError>fork + configure fds/env + execvp. Returns a Child the caller must eventually wait on.
Command::status(&this) -> Result<ExitStatus, IoError>spawn + wait. Stdin/out/err go wherever the caller configured (typically Inherit).
std::process::commandasync Command::collect(&this) -> Result<Output, IoError>Spawn, capture both streams, and await the exit without blocking the calling thread: the async spelling of output.
async Command::run(&this) -> Result<ExitStatus, IoError>Spawn and await the exit without capturing anything: the async spelling of status. Stdio goes wherever the caller configured it.
static ArgvBuf::new() -> ArgvBufCommand.program: StringCommand::env(mut &this, key: Str, value: Str) -> voidCommand.cwd: Option<String>Command::env_clear(mut &this) -> voidDon't inherit the parent's environment. Only the entries added with env() reach the child.
Command::stdin(mut &this, setting: Stdio) -> voidCommand::stderr(mut &this, setting: Stdio) -> voidCommand.args: Array<String>Command.envs: Array<EnvEntry>Command.clear_env_flag: booleanCommand.stdin_cfg: StdioCommand.stdout_cfg: StdioCommand.stderr_cfg: Stdiostatic Command::shell(command: string) -> i32Run command through the platform shell (/bin/sh -c on POSIX, cmd /c on Windows), inheriting stdio, and return the child's exit code (0 on success), or -1 if the shell could not be spawned or the child was killed by a signal.
extern "C" function system(command: u8*) -> i32new T { field: value }
new E::Variant(args)
new T[n]Heap allocation. new T { .. } places a struct literal on the heap and yields a T*, and new E::V(args) does the same for an enum variant; new T[n] reserves n uninitialised T - the typed malloc. The storage comes from T's Allocate implementation, or else the global allocator. Parentheses after a struct's name are refused (E0117): there are no constructors, and a struct's static new(..) is an ordinary method.
Child.stdin: Option<ChildStdin>Stdio::Fd(i32)Use a specific fd. Ownership transfers; the spawn closes the fd in the parent after duping it into the child.
std::process::commandimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference