Hello, everyone.
I have a simple program that uses pipeline |
on the command line to read from stdin and display it to the user for confirmation.
use std::io::{self, Read};
fn main() -> io::Result<()> {
println!("reading contents");
let contents = read_contents()?;
println!("stdin contents: {}", contents);
println!("Please make sure the input is correct:");
let input = read_user_confirmation()?;
println!("user input: {}", input);
Ok(())
}
fn read_user_confirmation() -> io::Result<String> {
let mut line = String::new();
io::stdin().read_line(&mut line)?;
Ok(line)
}
fn read_contents() -> io::Result<String> {
let mut contents = String::new();
io::stdin().read_to_string(&mut contents)?;
Ok(contents)
}
However, stdin cannot read user input correctly when using pipes on the command line. The problem is that the second time you read from stdin, you can't block the user's input and exit. like this
$ echo 'test contents' | target/debug/pipe-test
reading contents
stdin contents: test contents
Please make sure the input is correct:
user input:
The reference to If this function returns Ok(0), the stream has reached EOF
in std::io::BufRead::read_line may be read after the first EOF. I'm confused. Please help me