问题描述
我想用锈来执行tail -f a
,但是当我运行以下代码时没有输出:
fn main() {
// "a" is a text file and some characters have been written to it
let child = Command::new("tail").args(&["-f","a"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn().expect("Failed tail command");
let mut s = String::new();
child.stdout.expect("error of stdout")
.read_to_string(&mut s).expect("error of read all");
println!("{}",s);
}
当我在文件a
中添加新行时,我得到tail: a: file truncated
。
解决方法
read_to_string
读取直到EOF为止,因为tail
连续输出并且永不结束,所以它将永远不会被命中。更改程序以一次读取和打印一行。