pub struct Stdio(_);
Implementations§
source§impl Stdio
impl Stdio
sourcepub fn piped() -> Stdio
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 的程序时就是一个问题。
管道缓冲区的大小在不同的目标上有所不同。
sourcepub fn inherit() -> Stdio
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();
Runsourcepub fn null() -> Stdio
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), "");
// 忽略任何管道输入
RunTrait 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
将 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"
);
Run1.20.0 · source§impl From<ChildStdin> for Stdio
impl From<ChildStdin> for Stdio
source§fn from(child: ChildStdin) -> Stdio
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" 回显到控制台
Run1.20.0 · source§impl From<ChildStdout> for Stdio
impl From<ChildStdout> for Stdio
source§fn from(child: ChildStdout) -> Stdio
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");
Run1.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
将在引擎盖下使用 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");
Run1.63.0 · source§impl From<OwnedHandle> for Stdio
Available on Windows only.
impl From<OwnedHandle> for Stdio
Available on Windows only.
source§fn from(handle: OwnedHandle) -> Stdio
fn from(handle: OwnedHandle) -> Stdio
从输入类型转换为此类型。
1.2.0 · source§impl FromRawHandle for Stdio
Available on Windows only.
impl FromRawHandle for Stdio
Available on Windows only.