我想创建一个 rust 程序,它通过管道从外部程序输入并通过管道吐到另一个外部程序,就像三明治一样。更具体地说,我尝试创建的玩具模型是“gzip -cd input | rust | gzip -c -> output”。
基本上,我知道如何做第一部分(管道输入),例如:
let child = match Command::new("zcat")
.args(&["input"])
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn();
let filehand = BufReader::new(child.stdout.unwrap());
for line in filehand.lines() {
...
}
Run Code Online (Sandbox Code Playgroud)
但是我被困在第二部分,也不知道如何将两者拼凑起来。我知道 Perl 可以以优雅的方式做到这一点:
open filehand_in, "gzip -cd input |";
open filehand_out, "| gzip -c - > output";
while ( <filehand_in> ) {
# processing;
print filehand_out ...;
}
Run Code Online (Sandbox Code Playgroud)
我所说的优雅是指进出的外部处理对主程序是透明的:您只需要将第一个管道视为标准输入,将第二个管道视为标准输出,无需再担心。我想知道是否有人知道如何在 Rust 中实现类似的实现,谢谢。