Skip to content

Add eprint! and eprintln! #39228

Closed
Closed
@zackw

Description

@zackw

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions