Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std::process

ItemSummary
runRuns the program cmd with args, waits for it to finish, and returns what it wrote to standard output
run_attachedRuns the program cmd with args, waits for it to finish, and returns its exit code

Functions

run

fn run(cmd: str, args: [str], stdin: str?) -> str!

Runs the program cmd with args, waits for it to finish, and returns what it wrote to standard output. When stdin is given, it’s written to the program’s standard input.

cmd is looked up on the PATH, but there’s no shell in between. Each argument is passed to the program exactly as written. The output keeps its trailing newline, which trim removes.

Raises if the program can’t be started or its output isn’t valid UTF-8. A non-zero exit status raises as well, with the program’s standard error in the message.

use std::process;

let greeting = process::run("echo", ["hello"])!.trim(); // "hello"
let sorted = process::run("sort", [], "b\na\n")!;       // "a\nb\n"

run_attached

fn run_attached(cmd: str, args: [str]) -> int!

Runs the program cmd with args, waits for it to finish, and returns its exit code.

Unlike run, the program uses the script’s own standard input and output. Anything it prints appears directly (usually in the terminal) instead of coming back as a string, and a non-zero exit code is returned like any other. Raises if the program can’t be started or is stopped by a signal.

use std::process;

let code = process::run_attached("cargo", ["test"])!;
if code != 0 {
    print("tests failed");
}