std::process::Stdio - Rust
Description: API documentation for the Rust `Stdio` struct in crate `std`.
Keywords: rust, rustlang, rust-lang, Stdio

# 1.0.0[−][src]Struct std::process::Stdio

    pub struct 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`.

## Methods

### `impl Stdio`[src]

#### `pub fn piped() -> Stdio`[src]

A 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("Failed to execute command");

    assert_eq!(String::from_utf8_lossy(&output.stdout), "Hello, world!\n");
    // Nothing echoed to consoleRun

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");Run

#### `pub fn inherit() -> Stdio`[src]

The 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("Failed to execute command");

    assert_eq!(String::from_utf8_lossy(&output.stdout), "");
    // "Hello, world!" echoed to consoleRun

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();Run

#### `pub fn null() -> Stdio`[src]

This 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("Failed to execute command");

    assert_eq!(String::from_utf8_lossy(&output.stdout), "");
    // Nothing echoed to consoleRun

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), "");
    // Ignores any piped-in inputRun

## Trait Implementations

### `impl FromRawFd for Stdio`
1.2.0
[src]

#### `unsafe fn from_raw_fd(fd: RawFd) -> Stdio`[src]

This is supported on Unix only.

Constructs a new instance of `Self` from the given raw file descriptor. Read more

### `impl FromRawHandle for Stdio`
1.2.0
[src]

#### `unsafe fn from_raw_handle(handle: RawHandle) -> Stdio`[src]

This is supported on Windows only.

Constructs a new I/O object from the specified raw handle. Read more

### `impl From<ChildStdin> for Stdio`
1.20.0
[src]

#### `fn from(child: ChildStdin) -> Stdio`[src]

Converts a `ChildStdin` into a `Stdio`

# Examples

`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()) // Converted into a Stdio here
        .output()
        .expect("failed echo command");

    // "!dlrow ,olleH" echoed to consoleRun

### `impl From<ChildStdout> for Stdio`
1.20.0
[src]

#### `fn from(child: ChildStdout) -> Stdio`[src]

Converts a `ChildStdout` into a `Stdio`

# Examples

`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())  // Converted into a Stdio here
        .output()
        .expect("failed reverse command");

    assert_eq!(reverse.stdout, b"!dlrow ,olleH\n");Run

### `impl From<ChildStderr> for Stdio`
1.20.0
[src]

#### `fn from(child: ChildStderr) -> Stdio`[src]

Converts a `ChildStderr` into a `Stdio`

# Examples

    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()) // Converted into a Stdio here
        .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"
    );Run

### `impl From<File> for Stdio`
1.20.0
[src]

#### `fn from(file: File) -> Stdio`[src]

Converts a `File` into a `Stdio`

# Examples

`File` will be converted to `Stdio` using `Stdio::from` under the hood.

    use std::fs::File;
    use std::process::Command;

    // With the `foo.txt` file containing `Hello, world!"
    let file = File::open("foo.txt").unwrap();

    let reverse = Command::new("rev")
        .stdin(file)  // Implicit File conversion into a Stdio
        .output()
        .expect("failed reverse command");

    assert_eq!(reverse.stdout, b"!dlrow ,olleH");Run

### `impl Debug for Stdio`
1.16.0
[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

## Auto Trait Implementations

### `impl Send for Stdio`

### `impl Sync for Stdio`

## Blanket Implementations

### `impl<T> From for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom for T where
T: From<U>,` [src]

#### `type Error = !`

🔬 This is a nightly-only experimental API. (`try_from` #33417)

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

🔬 This is a nightly-only experimental API. (`try_from` #33417)

Performs the conversion.

### `impl<T, U> TryInto for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

🔬 This is a nightly-only experimental API. (`try_from` #33417)

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

🔬 This is a nightly-only experimental API. (`try_from` #33417)

Performs the conversion.

### `impl<T, U> Into for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T> Borrow for T where
T: ?Sized,` [src]

####
ⓘImportant traits for &'_ mut I

### Important traits for &'_ mut I

`impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'a, R: Read + ?Sized> Read for &'a mut Rimpl<'a, W: Write + ?Sized> Write for &'a mut W`
`fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut for T where
T: ?Sized,` [src]

####
ⓘImportant traits for &'_ mut I

### Important traits for &'_ mut I

`impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'a, R: Read + ?Sized> Read for &'a mut Rimpl<'a, W: Write + ?Sized> Write for &'a mut W`
`fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn get_type_id(&self) -> TypeId`[src]

🔬 This is a nightly-only experimental API. (`get_type_id` #27745)

this method will likely be replaced by an associated static

Gets the `TypeId` of `self`. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)