我不相信你可以做你真正想要的,現在做的事。理想情況下,您可以將Write
的實現者提供給Process::stdout
方法。不幸的是,set of choices for Stdio
是稀疏的。也許你可以競選讓這個功能成爲Rust 1.1的功能請求,或者創建一個箱子來開始充實一些細節(例如跨平臺兼容性)
如果刪除stdout/stderr的交錯,那麼這個解決方案可以幫助:
use std::io::{BufRead,BufReader};
use std::process::{Command,Stdio};
fn main() {
let mut child =
Command::new("/tmp/output")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn().unwrap();
if let Some(ref mut stdout) = child.stdout {
for line in BufReader::new(stdout).lines() {
let line = line.unwrap();
println!("[stdout] {}", line);
}
}
if let Some(ref mut stderr) = child.stderr {
for line in BufReader::new(stderr).lines() {
let line = line.unwrap();
println!("[stderr] {}", line);
}
}
let status = child.wait().unwrap();
println!("Finished with status {:?}", status);
}