1 个不稳定版本
使用旧的Rust 2015
0.1.0 | 2015年10月18日 |
---|
#978 in Unix API
5KB
81 行
pine
面向行的进程输出
apidocs
在此处找到它们 这里
用法
Rust 与进程交互的接口非常优秀,但有时您可能希望流式传输进程输出,而不是在获取完整进程 输出 之前等待进程退出。
对于这些用例,pine
提供了一个迭代器接口,遍历进程输出的行,表示为枚举 pine::Line::StdOut
或 pine::Line::StdErr
。这对于生成面向行输出的Unix程序非常适合。为了使程序能够访问这些输出行,需要确保子进程输出被“管道”到您的程序。Rust的Command接口使这变得简单。
extern crate pine;
use std::process::{Command, Stdio};
let mut process = Command::new("/bin/sh")
.arg("-c")
.arg("curl https://www.howsmyssl.com/a/check")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn().ok().unwrap();
将子进程的输出管道到您的程序后,您可以迭代输出行,直到它们可用。使用 pine::lines
函数。
use pine::Line;
let lines = pine::lines(&mut process);
for line in lines.iter() {
match line {
Line::StdOut(line) => println!("out -> {}", line),
Line::StdErr(line) => println!("err -> {}", line)
}
}
注意 iter()
返回一个迭代器,这意味着您可以使用迭代器上定义的任何函数来处理行输出。
Doug Tangren (softprops) 2015