Hi,
I am very new to Rust and I am currently trying to implement a simple program that get its data from a pipe.
The program description is the following:
- connect a program (A) that outputs data to my rust program (R) using a pipe:
A | R
. - the program (R) should consume the data line by line as they come.
I have written this simple code for now:
use std::io;
fn main() {
let mut count = 0;
loop {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("failed to read from pipe");
println!("in: {}", input);
if count > 5 {
break;
}
count += 1;
}
}
Unfortunately this program doesn't wait for the data and loops over and over:
in: hello
in: world
in:
in:
in:
in:
in:
Is there a way to make it block on receiving the data from the pipe? Something like a TCP stream listen().
Thanks