Describes what to do with a standard I/O stream for a child process when
passed to the stdin
, stdout
, and stderr
methods of Command
.
A new pipe should be arranged to connect the parent and child processes.
With stdout:
use std::process::{Command, Stdio};
let output = Command::new("echo")
.arg("Hello, world!")
.stdout(Stdio::piped())
.output()
.expect("Failed to execute command");
assert_eq!(String::from_utf8_lossy(&output.stdout), "Hello, world!\n");
With stdin:
use std::io::Write;
use std::process::{Command, Stdio};
let mut child = Command::new("rev")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Failed to spawn child process");
{
let mut stdin = child.stdin.as_mut().expect("Failed to open stdin");
stdin.write_all("Hello, world!".as_bytes()).expect("Failed to write to stdin");
}
let output = child.wait_with_output().expect("Failed to read stdout");
assert_eq!(String::from_utf8_lossy(&output.stdout), "!dlrow ,olleH\n");
The child inherits from the corresponding parent descriptor.
With stdout:
use std::process::{Command, Stdio};
let output = Command::new("echo")
.arg("Hello, world!")
.stdout(Stdio::inherit())
.output()
.expect("Failed to execute command");
assert_eq!(String::from_utf8_lossy(&output.stdout), "");
With stdin:
use std::process::{Command, Stdio};
use std::io::{self, Write};
let output = Command::new("rev")
.stdin(Stdio::inherit())
.stdout(Stdio::piped())
.output()
.expect("Failed to execute command");
print!("You piped in the reverse of: ");
io::stdout().write_all(&output.stdout).unwrap();
This stream will be ignored. This is the equivalent of attaching the
stream to /dev/null
With stdout:
use std::process::{Command, Stdio};
let output = Command::new("echo")
.arg("Hello, world!")
.stdout(Stdio::null())
.output()
.expect("Failed to execute command");
assert_eq!(String::from_utf8_lossy(&output.stdout), "");
With stdin:
use std::process::{Command, Stdio};
let output = Command::new("rev")
.stdin(Stdio::null())
.stdout(Stdio::piped())
.output()
.expect("Failed to execute command");
assert_eq!(String::from_utf8_lossy(&output.stdout), "");
Constructs a new instance of Self
from the given raw file descriptor. Read more
Formats the value using the given formatter. Read more
Converts a ChildStderr
into a Stdio
use std::process::{Command, Stdio};
let reverse = Command::new("rev")
.arg("non_existing_file.txt")
.stderr(Stdio::piped())
.spawn()
.expect("failed reverse command");
let cat = Command::new("cat")
.arg("-")
.stdin(reverse.stderr.unwrap())
.output()
.expect("failed echo command");
assert_eq!(
String::from_utf8_lossy(&cat.stdout),
"rev: cannot open non_existing_file.txt: No such file or directory\n"
);
Converts a ChildStdin
into a Stdio
ChildStdin
will be converted to Stdio
using Stdio::from
under the hood.
use std::process::{Command, Stdio};
let reverse = Command::new("rev")
.stdin(Stdio::piped())
.spawn()
.expect("failed reverse command");
let _echo = Command::new("echo")
.arg("Hello, world!")
.stdout(reverse.stdin.unwrap())
.output()
.expect("failed echo command");
Converts a File
into a Stdio
File
will be converted to Stdio
using Stdio::from
under the hood.
use std::fs::File;
use std::process::Command;
let file = File::open("foo.txt").unwrap();
let reverse = Command::new("rev")
.stdin(file)
.output()
.expect("failed reverse command");
assert_eq!(reverse.stdout, b"!dlrow ,olleH");
Converts a ChildStdout
into a Stdio
ChildStdout
will be converted to Stdio
using Stdio::from
under the hood.
use std::process::{Command, Stdio};
let hello = Command::new("echo")
.arg("Hello, world!")
.stdout(Stdio::piped())
.spawn()
.expect("failed echo command");
let reverse = Command::new("rev")
.stdin(hello.stdout.unwrap())
.output()
.expect("failed reverse command");
assert_eq!(reverse.stdout, b"!dlrow ,olleH\n");
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more