Skip to main content

How to Print to Standard Error in Rust with eprintln!

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

println! writes to standard output, where a program's real result goes. eprintln! writes to standard error, the channel for errors and warnings, so they still reach the screen when the output is redirected to a file.

Syntax​

Syntax
eprintln!("<format string>", <arguments>);
PartWhat it does
eprintln!Prints a line to standard error.
"<format string>"The text, with {} or {name} places for values, exactly as in println!.
<arguments>Optional. The values for the {} places.

The standard library's documentation for eprintln!: "Prints to the standard error, with a newline. Equivalent to the println! macro, except that output goes to io::stderr instead of io::stdout." It adds: "Use eprintln! only for error and progress messages. Use println! instead for the primary output of your program."

The Example​

The example is a tiny report job: it prints its result, Report saved., with println!, and a warning about skipped rows with eprintln!.

Prerequisites​

  • Rust, installed with rustup, which comes with Cargo. This guide uses Rust 1.100.0.

Step 1: Create a Project​

Terminal
cargo new demo
cd demo
Output
Creating binary (application) `demo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

cargo new demo creates a folder named demo with a Cargo.toml file, which describes the project, and src/main.rs, which holds the code. cd demo moves into it.

Step 2: Write the Code​

Replace the contents of src/main.rs with:

src/main.rs, which prints Report saved. with println! and Warning: 2 rows were skipped. with eprintln!.

src/main.rs
fn main() {
println!("Report saved.");
eprintln!("Warning: 2 rows were skipped.");
}

Step 3: Run It, Then Redirect the Output​

Run the program, then run it again with its output sent to a file, and look at the file:

Terminal
cargo run -q
cargo run -q > report.txt
cat report.txt
Output
Report saved.
Warning: 2 rows were skipped.
Warning: 2 rows were skipped.
Report saved.

A zsh terminal where cargo run -q prints both lines, cargo run -q > report.txt prints only the warning, and cat report.txt shows Report saved.

cargo run compiles the program and runs it, and -q hides Cargo's own status lines. The first run shows both lines. In the second, > sent standard output into report.txt, so only the warning stayed on screen. cat then shows the report line inside the file. In PowerShell 7.6 the same commands behave the same way: > redirects only standard output, and on Windows cat is an alias of Get-Content.

Capture the Warnings Instead​

2> redirects standard error. With cargo run -q 2> errors.txt, the screen shows only Report saved., and the file holds the warning:

Output
Report saved.
errors.txt
Warning: 2 rows were skipped.

Common Mistakes​

MistakeWhat happensDo this instead
Printing errors with println!They end up in the output file, mixed with the resultsUse eprintln! for errors and warnings
Printing the program's result with eprintln!> doesn't capture itUse println! for the result
Expecting eprintln! to stop the programIt only printsEnd with std::process::exit(1) when the program has to fail

Troubleshooting​

SymptomLikely causeFix
A message shows on screen although the output is redirectedIt goes to standard errorThat's the intent; redirect it with 2> if you need it in a file
The output file contains error messagesThey were printed with println!Switch them to eprintln!

FAQ​

Can eprintln! print values?​

Yes. It takes the same format string as println!, so eprintln!("Skipped {count} rows") works.

Where do Cargo's own messages go?​

Without -q, Cargo prints its status lines, such as Compiling, to standard error too, so they don't end up in a redirected output file.

Conclusion​

Use println! for a program's result and eprintln! for errors, warnings and progress. Redirecting with > then captures only the result, and 2> captures only the messages.

Additional Resources​

  • eprintln!, in the standard library documentation.
  • println!, in the standard library documentation.