rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.23k stars 12.7k forks source link

Add `eprint!` and `eprintln!` #39228

Closed zackw closed 7 years ago

zackw commented 7 years ago

From https://internals.rust-lang.org/t/extremely-pre-rfc-eprintln/4635

if you are writing command-line programs: the easiest way to emit non-fatal error messages is println!, but that sends them to stdout, which is not where error messages should go. panic! does the right thing (and thus also unwrap, expect, assert!, etc) but those are all (a) fatal and (b) properly used only for conditions that indicate a bug. The most straightforward way to write diagnostics to stderr is

writeln!(io::stderr(), "out of cheese error").unwrap();

which is significantly more typing in itself, requires you to use std::io::Write, and doesn't have println!'s defensive measures against being called at an awkward moment.

The simplest thing we could possibly add to fix this problem would be an eprintln! macro, which would be exactly the same as println! except that the output goes to stderr instead of stdout.

All of the documentation should also be updated to make it clear that it is inappropriate to use println! for error messages.

… which met with general approval and two people saying they already were adding eprintln! to all their own crates, so I'm just gonna go ahead and submit a PR. eprint! is included for completeness and symmetry; I'm not sure it's useful, but I think people would be surprised to find it missing.

petrochenkov commented 7 years ago

I'd suggest to just implement this and send a PR (in the worst case you'll need to write an RFC as well). This was suggested many times in various places, but nobody pushed hard enough and took responsibility to actually get this implemented and merged. I encourage you to do it. The number of likes this suggestion gathered on internals in one day shows that the feature is clearly wanted.

steveklabnik commented 7 years ago

This does require an RFC, as I mentioned in https://github.com/rust-lang/rust/pull/39229, so I'm going to give this a close; this would be tracked by the RFC process. Thanks!

musically-ut commented 7 years ago

If you are still running into this error, update Rust to version >= 1.21.0.


I spent a while googling this error and wondering which package providing eprintln! was old before realizing that this PR had eventually been merged in.