Writing to standard error

Rust:
Writing to standard error

How to:

Rust provides a straightforward way to write to stderr using the eprintln! macro, similar to how println! is used for stdout. Here’s a basic example:

fn main() {
    eprintln!("This is an error message!");
}

Sample output (to standard error):

This is an error message!

For more control over the error messages, such as when you want to format text or handle I/O results, use the stderr function from the std::io module. This method provides a handle to the global stderr stream, which you can then write to using methods like write_all or writeln from the Write trait:

use std::io::{self, Write};

fn main() {
    let stderr = io::stderr();
    let mut handle = stderr.lock();
    
    writeln!(handle, "Formatted error message: {}", 404).expect("Failed to write to stderr");
}

Sample output (to standard error):

Formatted error message: 404

If you are working in environments or applications where you rely on libraries for logging or error handling, libraries such as log and env_logger are popular. Though they are used more for logging purposes, they are configurable and can direct error log levels to stderr. Below is a simple usage example using log and env_logger:

First, add the dependencies to your Cargo.toml:

[dependencies]
log = "0.4"
env_logger = "0.9"

Then, setup and use the logging in your application:

fn main() {
    env_logger::init();
    log::error!("This is an error message logged to stderr");
}

Running this program (after setting up env_logger with an appropriate environment variable, for example, RUST_LOG=error) will output the error message to stderr, utilizing the logging infrastructure.

ERROR: This is an error message logged to stderr