pub struct Stdio(_);Expand description
Implementations
impl Stdio
source
impl Stdio
sourcepub fn piped() -> Stdio
source
pub fn piped() -> Stdio
sourceA 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 consoleRunWith 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");RunWriting 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.
pub fn inherit() -> Stdio
source
pub fn inherit() -> Stdio
sourceThe 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 consoleRunWith 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();Runpub fn null() -> Stdio
source
pub fn null() -> Stdio
sourceThis 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 consoleRunWith 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 inputRunTrait Implementations
impl From<ChildStderr> for Stdio
1.20.0 · source
impl From<ChildStderr> for Stdio
1.20.0 · sourcefn from(child: ChildStderr) -> Stdio
source
fn from(child: ChildStderr) -> Stdio
sourceConverts 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"
);Runimpl From<ChildStdin> for Stdio
1.20.0 · source
impl From<ChildStdin> for Stdio
1.20.0 · sourcefn from(child: ChildStdin) -> Stdio
source
fn from(child: ChildStdin) -> Stdio
sourceConverts 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 consoleRunimpl From<ChildStdout> for Stdio
1.20.0 · source
impl From<ChildStdout> for Stdio
1.20.0 · sourcefn from(child: ChildStdout) -> Stdio
source
fn from(child: ChildStdout) -> Stdio
sourceConverts 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");Runimpl From<File> for Stdio
1.20.0 · source
impl From<File> for Stdio
1.20.0 · sourcefn from(file: File) -> Stdio
source
fn from(file: File) -> Stdio
sourceExamples
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");Runimpl From<OwnedHandle> for Stdio
source Available on Windows only.
impl From<OwnedHandle> for Stdio
sourcefn from(handle: OwnedHandle) -> Stdio
source
fn from(handle: OwnedHandle) -> Stdio
sourceConverts to this type from the input type.
impl FromRawFd for Stdio
1.2.0 · source Available on Unix only.
impl FromRawFd for Stdio
1.2.0 · sourceunsafe fn from_raw_fd(fd: RawFd) -> Stdio
source
unsafe fn from_raw_fd(fd: RawFd) -> Stdio
sourceConstructs a new instance of Self from the given raw file
descriptor. Read more
impl FromRawHandle for Stdio
1.2.0 · source Available on Windows only.
impl FromRawHandle for Stdio
1.2.0 · sourceunsafe fn from_raw_handle(handle: RawHandle) -> Stdio
source
unsafe fn from_raw_handle(handle: RawHandle) -> Stdio
sourceConstructs a new I/O object from the specified raw handle. Read more
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
impl<T> BorrowMut<T> for T where
T: ?Sized,
source
impl<T> BorrowMut<T> for T where
T: ?Sized,
sourcefn borrow_mut(&mut self) -> &mut T
const: unstable · source
fn borrow_mut(&mut self) -> &mut T
const: unstable · sourceMutably borrows from an owned value. Read more