Since files and streams automatically close when dropped, but io::stdin()only provide a handle to the underlying stream, I don’t see how to explicitly close stdinor stdoutor detect EOF on stdinin my program.
Consider
fn main() {
let mut stdin = io::stdin();
let mut linebuffer = String::new();
loop {
match stdin.read_line(&mut linebuffer) {
Ok(i) if i == 0 => { break; },
Ok(i) => {
println!("{} {}", linebuffer, i);
},
Err(e) => { panic!(e); }
}
linebuffer.clear();
}
}
Checking the number of bytes placed in the buffer seems flaky, because the channel can be discarded with zero bytes that were written to it. Reading from closed stdinshould cause IOError, but it is not.
This is partly due to how to explicitly close your own stdout/ stderr?
source
share