I've been using the following function to read a single line of input in my program:
fn input() -> io::Result<String> {
let mut string = String::new();
let bytes_read = try!(io::stdin().read_line(&mut string));
if bytes_read == 0 {
return Err(io::Error::new(io::ErrorKind::Other,
"The line could not be read.");
}
string = string.trim_right_matches("\r\n").to_owned();
string = string.trim_right_matches("\n").to_owned();
return string;
}
If the number of bytes read is 0, then STDIN is closed. However, this function doesn't work: it allows a single line of empty input to be returned after STDIN has been closed. Only after that does the bytes_read
error kick in.
Is it possible to modify this function to return an error right after STDIN is closed?