Rust Reference ยท Macros
Rust eprintln! and eprint! macros
Reference for the Rust eprintln! and eprint! macros: writing diagnostics to standard error, how stderr differs from stdout, redirection and exit codes.
eprintln! and eprint! are the standard-error versions of println! and print!. They accept the same format strings and arguments, but the text goes to stderr, the stream meant for errors, warnings and diagnostics, while stdout is kept for the real output of the program.
Keeping the two apart lets users pipe or redirect a program's results (prog > out.txt) without error messages ending up in the file, and lets scripts show errors on the terminal while capturing output.
eprintln!("warning: config file not found, using defaults");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
eprintln!(); eprintln!("format string", args...); eprint!("format string", args...);
| Name | Description |
|---|---|
| format string | Same syntax as for println!. |
args |
Positional, named or inline-captured values. |
Returns: ().
Errors / panics / edge cases: like println! they panic if a write fails, but a closed stderr is silently ignored and the output is dropped. Format errors are compile-time errors, as with every formatting macro.
Usage notes
- Unbuffered: stderr is not buffered, so the text appears immediately and in the right order relative to a crash, which is what you want for diagnostics. Each call may therefore be slower than a
println!in a tight loop. - Interleaving: because the streams are separate, the order in which stdout and stderr text shows up in a terminal is not guaranteed once both are redirected or buffered.
- Exit codes: printing an error does not stop the program. Return a non-zero
ExitCodefrommain, or callstd::process::exit(1), so callers can detect failure. - Returning
Errfrommainprints theDebugform of the error to stderr and exits with code 1, which is often enough for small tools. - Logging: real applications use a logging crate with levels and timestamps;
eprintln!is the dependency-free option for small programs and for debugging. - Tests: test harnesses capture stderr output of
eprintln!along with stdout for passing tests. - Panics also write to stderr, formatted by the panic hook, not through this macro.
- Locking: use
io::stderr().lock()withwriteln!for several lines that must not be separated by other threads. - Do not print secrets in error messages; they end up in logs and terminals.
Related
println!andprint!,format!,dbg!,panic!,Termination- Lessons: Printing, Main and exit codes