child
import std::process::child; · source
Types
Child
type struct Child {
stdin: Option<ChildStdin>;
stdout: Option<ChildStdout>;
stderr: Option<ChildStderr>;
static new(pid: i32, handle: u64, stdin: Option<ChildStdin>, stdout: Option<ChildStdout>, stderr: Option<ChildStderr>) -> Child;
id(&this) -> i32;
wait(mut &this) -> Result<ExitStatus, IoError>;
async join(mut &this) -> Result<ExitStatus, IoError>;
try_wait(mut &this) -> Result<Option<ExitStatus>, IoError>;
kill(&this) -> Result<(), IoError>;
send_signal(&this, sig: Signal) -> Result<(), IoError>;
}
A running subprocess, produced by spawn. It holds the pid (private, behind id()) and whichever pipe ends you asked for.
wait blocks and closes stdin first so the child sees EOF; join is the non-blocking spelling, parking on the reactor (a pidfd on Linux) or the blocking pool; try_wait returns Ok(None) while the child is still running. kill is SIGKILL on POSIX and does not wait.
wait closing stdin first is deliberate: it is deadlock-avoidant for a child that reads all of stdin before writing anything to stdout. join does not touch the pipes, so a child that writes more than a pipe buffer holds must have its stdout / stderr drained while you await — the same hazard, and the reason Command::collect drains first.
Properties
| Property | Type |
|---|---|
stdin | Option<ChildStdin> |
stdout | Option<ChildStdout> |
stderr | Option<ChildStderr> |
Constructors
static new(pid: i32, handle: u64, stdin: Option<ChildStdin>, stdout: Option<ChildStdout>, stderr: Option<ChildStderr>) -> Child;
static new(pid: i32, handle: u64, stdin: Option<ChildStdin>, stdout: Option<ChildStdout>, stderr: Option<ChildStderr>) -> Child;Low-level constructor used by Command::spawn. Application code should not call this directly. handle is 0 on POSIX and the Win32 process HANDLE on Windows.
Methods
id(&this) -> i32;
wait(mut &this) -> Result<ExitStatus, IoError>;
wait(mut &this) -> Result<ExitStatus, IoError>;Block until the child exits. Closes stdin first so the child sees EOF; this is deadlock-avoidant for children that read all of stdin before writing to stdout.
async join(mut &this) -> Result<ExitStatus, IoError>;
async join(mut &this) -> Result<ExitStatus, IoError>;Await the child's exit without blocking the calling thread: the async spelling of wait. Same stdin-close, same status, same waited bookkeeping — but the task parks and the executor worker stays free. ChildWait documents which of the two routes each platform takes.
try_wait(mut &this) -> Result<Option<ExitStatus>, IoError>;
try_wait(mut &this) -> Result<Option<ExitStatus>, IoError>;Non-blocking check. Ok(Some(status)) means the child has exited; Ok(None) means it's still running.
kill(&this) -> Result<(), IoError>;
kill(&this) -> Result<(), IoError>;Send SIGKILL (POSIX) / terminate the process (Windows). Does not wait; call wait afterwards to reap the zombie on POSIX.
send_signal(&this, sig: Signal) -> Result<(), IoError>;
The pipe ends
type struct ChildStdin {
fd: i32;
static from_fd(raw_fd: i32) -> ChildStdin;
}
type struct ChildStdout {
fd: i32;
static from_fd(raw_fd: i32) -> ChildStdout;
}
type struct ChildStderr {
fd: i32;
static from_fd(raw_fd: i32) -> ChildStderr;
}
The parent's ends of the pipes a Stdio::Piped configuration created. ChildStdin implements Write; ChildStdout and ChildStderr implement Read. Dropping ChildStdin closes the pipe, which is what signals EOF to the child.
ChildStdin
Properties
| Property | Type |
|---|---|
fd | i32 |
Constructors
####### static from_fd(raw_fd: i32) -> ChildStdin;
ChildStdout
Properties
| Property | Type |
|---|---|
fd | i32 |
Constructors
####### static from_fd(raw_fd: i32) -> ChildStdout;
ChildStderr
Properties
| Property | Type |
|---|---|
fd | i32 |
Constructors
####### static from_fd(raw_fd: i32) -> ChildStderr;
Reaping
Every Child should have wait — or kill then wait — called before it drops.
Child::drop closes any captured pipe ends and makes a non-blocking best-effort reap. If the child has already exited, its zombie is released there. If it is still running, a destructor cannot block, so the child stays detached and reaping it remains your responsibility. That closes the common "spawn, child finishes, drop without wait" leak without ever blocking. Cancelling a join mid-wait leaves the child in exactly this state.
Trait implementations
Child
implement trait Drop for struct Child
ChildStdin
implement trait Write for struct ChildStdin
implement trait Drop for struct ChildStdin
ChildStdout
implement trait Read for struct ChildStdout
implement trait Drop for struct ChildStdout
ChildStderr
implement trait Read for struct ChildStderr
implement trait Drop for struct ChildStderr
ExitStatus
type struct ExitStatus {
code: i32;
signal: i32;
signaled: boolean;
success(&this) -> boolean;
exit_code(&this) -> Option<i32>;
terminating_signal(&this) -> Option<i32>;
}
POSIX packs a normal exit and a signal death into one status word; ExitStatus is the decoded form.
success() is an exit with status 0. exit_code() is None and terminating_signal() is Some when a signal ended the process.
Properties
| Property | Type |
|---|---|
code | i32 |
signal | i32 |
signaled | boolean |
Methods
success(&this) -> boolean;
success(&this) -> boolean;Convenience: did the child exit with status 0?
exit_code(&this) -> Option<i32>;
exit_code(&this) -> Option<i32>;Some(code) for a normal exit, None if the child was killed by a signal.
terminating_signal(&this) -> Option<i32>;
terminating_signal(&this) -> Option<i32>;Some(signal) if the child was killed by a signal.
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 Child {
stdin: Option<ChildStdin>;
stdout: Option<ChildStdout>;
stderr: Option<ChildStderr>;
}Child: a running subprocess.
Child.stdin: Option<ChildStdin>type struct ChildStdin {
fd: i32;
}Write end of a pipe whose read end is the child's stdin.
std::process::childChild.stdout: Option<ChildStdout>type struct ChildStdout {
fd: i32;
}Read end of a pipe whose write end is the child's stdout.
std::process::childChild.stderr: Option<ChildStderr>type struct ChildStderr {
fd: i32;
}Read end of a pipe whose write end is the child's stderr.
std::process::childstatic 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 Child::new(pid: i32, handle: u64, stdin: Option<ChildStdin>, stdout: Option<ChildStdout>, stderr: Option<ChildStderr>) -> ChildLow-level constructor used by Command::spawn. Application code should not call this directly. handle is 0 on POSIX and the Win32 process HANDLE on Windows.
(parameter) pid: i32(parameter) handle: u64(parameter) stdin: Option<ChildStdin>(parameter) stdout: Option<ChildStdout>(parameter) stderr: Option<ChildStderr>type struct Child {
stdin: Option<ChildStdin>;
stdout: Option<ChildStdout>;
stderr: Option<ChildStderr>;
}Child: a running subprocess.
std::process::childChild::id(&this) -> i32&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.
Child::wait(mut &this) -> Result<ExitStatus, IoError>Block until the child exits. Closes stdin first so the child sees EOF; this is deadlock-avoidant for children that read all of stdin before writing to stdout.
mut &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.
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.
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}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 Child::join(mut &this) -> Result<ExitStatus, IoError>Await the child's exit without blocking the calling thread: the async spelling of wait. Same stdin-close, same status, same waited bookkeeping — but the task parks and the executor worker stays free. ChildWait documents which of the two routes each platform takes.
Child::try_wait(mut &this) -> Result<Option<ExitStatus>, IoError>Non-blocking check. Ok(Some(status)) means the child has exited; Ok(None) means it's still running.
Child::kill(&this) -> Result<(), IoError>Send SIGKILL (POSIX) / terminate the process (Windows). Does not wait; call wait afterwards to reap the zombie on POSIX.
primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
Child::send_signal(&this, sig: Signal) -> Result<(), IoError>(parameter) sig: Signaltype struct ChildStdin {
fd: i32;
}Write end of a pipe whose read end is the child's stdin.
ChildStdin.fd: i32static ChildStdin::from_fd(raw_fd: i32) -> ChildStdin(parameter) raw_fd: i32type struct ChildStdout {
fd: i32;
}Read end of a pipe whose write end is the child's stdout.
ChildStdout.fd: i32static ChildStdout::from_fd(raw_fd: i32) -> ChildStdouttype struct ChildStderr {
fd: i32;
}Read end of a pipe whose write end is the child's stderr.
ChildStderr.fd: i32static ChildStderr::from_fd(raw_fd: i32) -> ChildStderrtype 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.
ExitStatus.code: i32ExitStatus.signal: i32ExitStatus.signaled: booleanExitStatus::success(&this) -> booleanConvenience: did the child exit with status 0?
ExitStatus::exit_code(&this) -> Option<i32>Some(code) for a normal exit, None if the child was killed by a signal.
ExitStatus::terminating_signal(&this) -> Option<i32>Some(signal) if the child was killed by a signal.
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).
Child::id(&this) -> i32Child::wait(mut &this) -> Result<ExitStatus, IoError>Block until the child exits. Closes stdin first so the child sees EOF; this is deadlock-avoidant for children that read all of stdin before writing to stdout.
std::process::childasync Child::join(mut &this) -> Result<ExitStatus, IoError>Await the child's exit without blocking the calling thread: the async spelling of wait. Same stdin-close, same status, same waited bookkeeping — but the task parks and the executor worker stays free. ChildWait documents which of the two routes each platform takes.
Child::try_wait(mut &this) -> Result<Option<ExitStatus>, IoError>Non-blocking check. Ok(Some(status)) means the child has exited; Ok(None) means it's still running.
Result::Ok(T)Option::NoneChild::kill(&this) -> Result<(), IoError>Send SIGKILL (POSIX) / terminate the process (Windows). Does not wait; call wait afterwards to reap the zombie on POSIX.
Child.stdout: Option<ChildStdout>Child.stderr: Option<ChildStderr>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.
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.
Child.stdin: Option<ChildStdin>static Child::new(pid: i32, handle: u64, stdin: Option<ChildStdin>, stdout: Option<ChildStdout>, stderr: Option<ChildStderr>) -> ChildLow-level constructor used by Command::spawn. Application code should not call this directly. handle is 0 on POSIX and the Win32 process HANDLE on Windows.
Command::spawn(&this) -> Result<Child, IoError>fork + configure fds/env + execvp. Returns a Child the caller must eventually wait on.
ChildWaitJob.handle: u64type struct ChildWait {
pid: i32;
handle: u64;
exit_fd: i32;
rt: Reactor*;
job: ChildWaitJob*;
routed: boolean;
}Wait for a child to exit without stalling the executor. The machinery behind Child::join; await that rather than driving this directly, because only the method updates the Child's bookkeeping.
Option::Some(T)ChildWaitJob.status: ExitStatusChild::send_signal(&this, sig: Signal) -> Result<(), IoError>type enum Stdio {
Inherit;
Null;
Piped;
Fd(i32);
}How a child's stdin / stdout / stderr is wired up.
std::process::commandStdio::PipedCreate a pipe; the parent keeps its end in the resulting Child::stdin / stdout / stderr.
type trait Write {
write_some(mut &this, bytes: Slice<u8>) -> Result<u64, IoError>;
flush(mut &this) -> Result<(), IoError>;
write<T>(mut &this, data: T) -> Result<(), IoError>;
}type trait Read {
read(mut &this, buffer: u8*, length: u64) -> Result<u64, IoError>;
read_all(mut &this, buffer: u8*, length: u64) -> Result<u64, IoError>;
read_byte(mut &this) -> Result<Option<u8>, IoError>;
read_char(mut &this) -> Result<Option<u32>, IoError>;
read_until(mut &this, delimiter: u8, out: mut &Array<u8>) -> Result<u64, IoError>;
read_exact(mut &this, buffer: u8*, length: u64) -> Result<(), IoError>;
read_to_end(mut &this, out: mut &Array<u8>) -> Result<u64, IoError>;
read_to_string(mut &this, out: mut &String) -> Result<u64, IoError>;
read_line(mut &this, out: mut &String) -> Result<u64, IoError>;
}ChildStdin.fd: i32static ChildStdin::from_fd(raw_fd: i32) -> ChildStdinimplement 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 referenceExitStatus::success(&this) -> booleanConvenience: did the child exit with status 0?
std::process::childExitStatus::exit_code(&this) -> Option<i32>Some(code) for a normal exit, None if the child was killed by a signal.
ExitStatus::terminating_signal(&this) -> Option<i32>Some(signal) if the child was killed by a signal.
ExitStatus.code: i32ExitStatus.signal: i32ExitStatus.signaled: boolean