Struct std::process::Stdio

1.0.0 · source ·
pub struct Stdio(_);
Expand description

描述当传递给 Commandstdinstdoutstderr 方法时,如何对子进程使用标准 I/O 流。

Implementations§

source§

impl Stdio

source

pub fn piped() -> Stdio

应该安排一个新管道来连接父进程和子进程。

Examples

使用 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");
// 控制台没有回显
Run

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

在不同时读取 stdout 和 stderr 的情况下,向 stdin 写入超过管道缓冲区的输入值可能会导致死锁。 这在运行任何不能保证在写入超过管道缓冲区的输出值之前不能保证读取其整个 stdin 的程序时就是一个问题。

管道缓冲区的大小在不同的目标上有所不同。

source

pub fn inherit() -> Stdio

子级从相应的父级描述符继承。

Examples

使用 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!" 回显到控制台
Run

使用 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
source

pub fn null() -> Stdio

此流将被忽略。 这等效于将流附加到 /dev/null

Examples

使用 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), "");
// 控制台没有回显
Run

使用 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), "");
// 忽略任何管道输入
Run
source

pub fn makes_pipe(&self) -> bool

🔬This is a nightly-only experimental API. (stdio_makes_pipe #98288)

如果这需要 Command 创建新管道,则返回 true

Example
#![feature(stdio_makes_pipe)]
use std::process::Stdio;

let io = Stdio::piped();
assert_eq!(io.makes_pipe(), true);
Run

Trait Implementations§

1.16.0 · source§

impl Debug for Stdio

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化程序格式化该值。 Read more
1.20.0 · source§

impl From<ChildStderr> for Stdio

source§

fn from(child: ChildStderr) -> Stdio

ChildStderr 转换为 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()) // 在此处转换为 Stdio
    .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
1.20.0 · source§

impl From<ChildStdin> for Stdio

source§

fn from(child: ChildStdin) -> Stdio

ChildStdin 转换为 Stdio

Examples

ChildStdin 将在引擎盖下使用 Stdio::from 转换为 Stdio

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()) // 在此处转换为 Stdio
    .output()
    .expect("failed echo command");

// "!dlrow ,olleH" 回显到控制台
Run
1.20.0 · source§

impl From<ChildStdout> for Stdio

source§

fn from(child: ChildStdout) -> Stdio

ChildStdout 转换为 Stdio

Examples

ChildStdout 将在引擎盖下使用 Stdio::from 转换为 Stdio

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())  // 在此处转换为 Stdio
    .output()
    .expect("failed reverse command");

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

impl From<File> for Stdio

source§

fn from(file: File) -> Stdio

File 转换为 Stdio

Examples

File 将在引擎盖下使用 Stdio::from 转换为 Stdio

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

// 使用包含 "Hello, world!" 的 `foo.txt` 文件
let file = File::open("foo.txt").unwrap();

let reverse = Command::new("rev")
    .stdin(file)  // 隐式文件转换为 Stdio
    .output()
    .expect("failed reverse command");

assert_eq!(reverse.stdout, b"!dlrow ,olleH");
Run
1.63.0 · source§

impl From<OwnedFd> for Stdio

Available on Unix only.
source§

fn from(fd: OwnedFd) -> Stdio

从输入类型转换为此类型。
1.63.0 · source§

impl From<OwnedHandle> for Stdio

Available on Windows only.
source§

fn from(handle: OwnedHandle) -> Stdio

从输入类型转换为此类型。
1.2.0 · source§

impl FromRawFd for Stdio

Available on Unix only.
source§

unsafe fn from_raw_fd(fd: RawFd) -> Stdio

根据给定的原始文件描述符构造 Self 的新实例。 Read more
1.2.0 · source§

impl FromRawHandle for Stdio

Available on Windows only.
source§

unsafe fn from_raw_handle(handle: RawHandle) -> Stdio

从指定的原始句柄创建一个新的 I/O 对象。 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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

获取 selfTypeIdRead more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

从拥有的值中一成不变地借用。 Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

从拥有的值中借用。 Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

返回未更改的参数。

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

调用 U::from(self)

也就是说,这种转换是 From<T> for U 实现选择执行的任何操作。

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

发生转换错误时返回的类型。
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

发生转换错误时返回的类型。
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。