Description
Right now print!()
and println!()
explicitly panic when they get an error writing to stdout. This is undocumented. It's also surprising, partially because printing to stdout is not usually considered "critical" functionality for a program, and partially because all precedent I'm aware of in other languages for "print this thing to stdout" does not abort if the print fails.
Anyone printing information to stdout that is "critical", such that the program should not continue if the write failed, should be using write!()
or writeln!()
(or one of the io::Write
methods) in order to handle the error (which could just be calling .unwrap()
to panic if the write failed). But I fully expect that the vast majority of code using println!()
is not printing something that is so critical that the program should refuse to continue if the print fails, and as such should simply ignore any errors instead of panicking. Ultimately, println!()
is the "convenience" way to print informative stuff to stdout, and panicking on EPIPE is not very convenient.
There was some discussion a year ago on #13824 that floated the idea of ignoring the error, but it seemed fairly inconclusive. And another ticket #14505 shows that rustc
reports an ICE when invoked as rustc --version | false
(due to false
terminating without reading stdin).
Conceptually, I believe that println!(...)
should be equivalent to let _ = writeln!(io::stdout(), ...)
, although in practice it won't be because std::io
still has the task-local stdout replacement functionality (used by libtest).
/cc @alexcrichton