Struct Stdio
pub struct Stdio(pub(in ::process) Stdio);
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.
Fields
0: Stdio
Implementations
impl Stdio
fn piped() -> StdioA new pipe should be arranged to connect the parent and child processes.
Examples
With stdout:
use std::process::{Command, Stdio}; let output = Command::new("echo") .arg("Hello, world!") .stdout(Stdio::piped()) .output() .expect("process should execute successfully"); assert_eq!(String::from_utf8_lossy(&output.stdout), "Hello, world!\n"); // Nothing echoed to consoleWith stdin:
use std::io::Write; use std::process::{Command, Stdio}; let mut child = Command::new("rev") .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() .expect("rev command should start"); let mut stdin = child.stdin.take().expect("child stdin should be retrievable"); std::thread::spawn(move || { stdin.write_all("Hello, world!".as_bytes()).expect("writing to child stdin should succeed"); }); let output = child.wait_with_output().expect("child stdout should be able to be read"); assert_eq!(String::from_utf8_lossy(&output.stdout), "!dlrow ,olleH");Writing more than a pipe buffer's worth of input to stdin without also reading stdout and stderr at the same time may cause a deadlock. This is an issue when running any program that doesn't guarantee that it reads its entire stdin before writing more than a pipe buffer's worth of output. The size of a pipe buffer varies on different targets.
fn inherit() -> StdioThe child inherits from the corresponding parent descriptor.
Examples
With stdout:
use std::process::{Command, Stdio}; let output = Command::new("echo") .arg("Hello, world!") .stdout(Stdio::inherit()) .output() .expect("process should execute successfully"); assert_eq!(String::from_utf8_lossy(&output.stdout), ""); // "Hello, world!" echoed to consoleWith stdin:
use std::process::{Command, Stdio}; use std::io::{self, Write}; let output = Command::new("rev") .stdin(Stdio::inherit()) .stdout(Stdio::piped()) .output()?; print!("You piped in the reverse of: "); io::stdout().write_all(&output.stdout)?; # io::Result::Ok(())fn null() -> StdioThis stream will be ignored. This is the equivalent of attaching the stream to
/dev/null.Examples
With stdout:
use std::process::{Command, Stdio}; let output = Command::new("echo") .arg("Hello, world!") .stdout(Stdio::null()) .output() .expect("process should execute successfully"); assert_eq!(String::from_utf8_lossy(&output.stdout), ""); // Nothing echoed to consoleWith stdin:
use std::process::{Command, Stdio}; let output = Command::new("rev") .stdin(Stdio::null()) .stdout(Stdio::piped()) .output() .expect("process should execute successfully"); assert_eq!(String::from_utf8_lossy(&output.stdout), ""); // Ignores any piped-in inputfn makes_pipe(&self) -> boolReturns
trueif this requiresCommandto create a new pipe.Example
use Stdio; let io = piped; assert_eq!;
Trait Implementations
impl Debug for Stdio
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl From<ChildStderr> for Stdio
fn from(child: ChildStderr) -> StdioConverts a
ChildStderrinto aStdio.Examples
use ; let reverse = new .arg .stderr .spawn .expect; let cat = new .arg .stdin // Converted into a Stdio here .output .expect; assert_eq!;
impl From<ChildStdin> for Stdio
fn from(child: ChildStdin) -> StdioConverts a
ChildStdininto aStdio.Examples
ChildStdinwill be converted toStdiousingStdio::fromunder the hood.use ; let reverse = new .stdin .spawn .expect; let _echo = new .arg .stdout // Converted into a Stdio here .output .expect; // "!dlrow ,olleH" echoed to console
impl From<ChildStdout> for Stdio
fn from(child: ChildStdout) -> StdioConverts a
ChildStdoutinto aStdio.Examples
ChildStdoutwill be converted toStdiousingStdio::fromunder the hood.use ; let hello = new .arg .stdout .spawn .expect; let reverse = new .stdin // Converted into a Stdio here .output .expect; assert_eq!;
impl From<File> for Stdio
fn from(file: File) -> Stdio-
Examples
Filewill be converted toStdiousingStdio::fromunder the hood.use File; use Command; // With the `foo.txt` file containing "Hello, world!" let file = open?; let reverse = new .stdin // Implicit File conversion into a Stdio .output?; assert_eq!; # Ok
impl From<OwnedFd> for Stdio
fn from(fd: OwnedFd) -> StdioTakes ownership of a file descriptor and returns a
Stdiothat can attach a stream to it.
impl From<OwnedHandle> for Stdio
fn from(handle: OwnedHandle) -> StdioTakes ownership of a handle and returns a
Stdiothat can attach a stream to it.
impl From<PipeReader> for Stdio
fn from(pipe: PipeReader) -> Self
impl From<PipeWriter> for Stdio
fn from(pipe: PipeWriter) -> Self
impl From<Stderr> for Stdio
fn from(inherit: Stderr) -> StdioRedirect command stdout/stderr to our stderr
Examples
use io; use Command; # # # if cfg!
impl From<Stdout> for Stdio
fn from(inherit: Stdout) -> StdioRedirect command stdout/stderr to our stdout
Examples
use io; use Command; # # # if cfg!
impl FromInner<Stdio> for Stdio
fn from_inner(inner: Stdio) -> Stdio
impl FromRawFd for Stdio
unsafe fn from_raw_fd(fd: RawFd) -> Stdio
impl FromRawHandle for Stdio
unsafe fn from_raw_handle(handle: RawHandle) -> Stdio
Auto Trait Implementations
impl Freeze for Stdio
impl RefUnwindSafe for Stdio
impl Send for Stdio
impl Sync for Stdio
impl Unpin for Stdio
impl UnsafeUnpin for Stdio
impl UnwindSafe for Stdio
Blanket Implementations
impl<T> Any for Stdio
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Stdio
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Stdio
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Stdio
fn from(t: T) -> TReturns the argument unchanged.
impl<T> SizeHint for Stdio
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>
impl<T> SizedTypeProperties for Stdio
impl<T, U> Into<U> for Stdio
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Stdio
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Stdio
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>