pub struct Stdio(/* private fields */);
Expand description
Implementations§
source§impl Stdio
impl Stdio
sourcepub fn piped() -> Stdio
pub fn piped() -> Stdio
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 console
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.take().expect("Failed to open stdin");
std::thread::spawn(move || {
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");
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.
sourcepub fn inherit() -> Stdio
pub fn inherit() -> Stdio
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 console
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();
sourcepub fn null() -> Stdio
pub fn null() -> Stdio
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 console
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 input
sourcepub fn makes_pipe(&self) -> bool
🔬This is a nightly-only experimental API. (stdio_makes_pipe
)
pub fn makes_pipe(&self) -> bool
stdio_makes_pipe
)Trait Implementations§
1.20.0 · source§impl From<ChildStderr> for Stdio
impl From<ChildStderr> for Stdio
source§fn from(child: ChildStderr) -> Stdio
fn from(child: ChildStderr) -> Stdio
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"
);
1.20.0 · source§impl From<ChildStdin> for Stdio
impl From<ChildStdin> for Stdio
source§fn from(child: ChildStdin) -> Stdio
fn from(child: ChildStdin) -> Stdio
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 console
1.20.0 · source§impl From<ChildStdout> for Stdio
impl From<ChildStdout> for Stdio
source§fn from(child: ChildStdout) -> Stdio
fn from(child: ChildStdout) -> Stdio
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");
1.20.0 · source§impl From<File> for Stdio
impl From<File> for Stdio
source§fn from(file: File) -> Stdio
fn from(file: File) -> 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");
Auto Trait Implementations§
impl RefUnwindSafe for Stdio
impl Send for Stdio
impl Sync for Stdio
impl Unpin for Stdio
impl UnwindSafe for Stdio
Blanket Implementations§
source§impl<T> Also for T
impl<T> Also for T
source§impl<T> AnyExt for Twhere
T: Any,
impl<T> AnyExt for Twhere T: Any,
source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
any
only.self
. Read moresource§fn as_any_ref(&self) -> &dyn Anywhere
Self: Sized,
fn as_any_ref(&self) -> &dyn Anywhere Self: Sized,
any
only.source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere Self: Sized,
any
only.source§impl<T, Res> Apply<Res> for Twhere
T: ?Sized,
impl<T, Res> Apply<Res> for Twhere T: ?Sized,
source§fn apply<F: FnOnce(Self) -> Res>(self, f: F) -> Reswhere
Self: Sized,
fn apply<F: FnOnce(Self) -> Res>(self, f: F) -> Reswhere Self: Sized,
result
only.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
source§impl<T> Mem for Twhere
T: ?Sized,
impl<T> Mem for Twhere T: ?Sized,
source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
mem
only.source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
mem
only.true
if dropping values of this type matters.source§fn mem_drop(self)where
Self: Sized,
fn mem_drop(self)where Self: Sized,
mem
only.self
by running its destructor.source§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where Self: Sized,
mem
only.self
without running its destructor.source§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere Self: Sized,
mem
only.self
with other, returning the previous value of self
.source§fn mem_take(&mut self) -> Selfwhere
Self: Default,
fn mem_take(&mut self) -> Selfwhere Self: Default,
mem
only.self
with its default value, returning the previous value of self
.source§fn mem_swap(&mut self, other: &mut Self)where
Self: Sized,
fn mem_swap(&mut self, other: &mut Self)where Self: Sized,
mem
only.self
and other
without deinitializing either one.source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where T: OverflowingCast<Dst>,
source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere Src: OverflowingCast<Dst>,
source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere T: SaturatingCast<Dst>,
source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere Src: SaturatingCast<Dst>,
source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
source§impl<T> Size for T
impl<T> Size for T
source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
mem
only.source§const BYTE_SIZE: usize = _
const BYTE_SIZE: usize = _
mem
only.source§const PTR_SIZE: usize = 4usize
const PTR_SIZE: usize = 4usize
mem
only.source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
mem
only.