{"slug": "rust-guide-12-8-writing-error-messages-to-standard-error", "title": "[Rust Guide]12.8. Writing Error Messages to Standard Error", "summary": "This article explains how to modify a Rust command-line grep program to separate error messages from standard output by using the `eprintln!` macro instead of `println!`. The key advantage is that when normal output is redirected to a file using `cargo run > output.txt`, error messages will still appear on the screen rather than being written to the file. The change only requires modifying `main.rs` to use `eprintln!` for all error handling, leaving `lib.rs` unchanged.", "body_md": "## 12.8.0 Before We Begin\n\nChapter 12 builds a sample project: a command-line program. The program is `grep`\n\n(**Global Regular Expression Print**), a tool for global regular-expression searching and output. Its function is to **search for specified text in a specified file**.\n\nThis project is divided into these steps:\n\n- Receiving command-line arguments\n- Reading files\n- Refactoring: improving modules and error handling\n- Using TDD (test-driven development) to develop library functionality\n- Using environment variables\n**Writing error messages to standard error instead of standard output (this article)**\n\n**If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.**\n\n## 12.8.1 Review\n\nHere is all the code written up to the previous article.\n\n`lib.rs`\n\n:\n\n```\nuse std::error::Error;\nuse std::fs;\n\npub struct Config {\n    pub query: String,\n    pub filename: String,\n    pub case_sensitive: bool,\n}\n\nimpl Config {\n    pub fn new(args: &[String]) -> Result<Config, &'static str> {\n        if args.len() < 3 {\n            return Err(\"Not enough arguments\");\n        }\n        let query = args[1].clone();\n        let filename = args[2].clone();\n        let case_sensitive = std::env::var(\"IGNORE_CASE\").is_err();\n        Ok(Config {\n            query,\n            filename,\n            case_sensitive,\n        })\n    }\n}\n\npub fn run(config: Config) -> Result<(), Box<dyn Error>> {\n    let contents = fs::read_to_string(config.filename)?;\n    let results = if config.case_sensitive {\n        search(&config.query, &contents)\n    } else {\n        search_case_insensitive(&config.query, &contents)\n    };\n    for line in results {\n        println!(\"{}\", line);\n    }\n    Ok(())\n}\n\npub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {\n    let mut results = Vec::new();\n    for line in contents.lines() {\n        if line.contains(query) {\n            results.push(line);\n        }\n    }\n    results\n}\n\npub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {\n    let mut results = Vec::new();\n    let query = query.to_lowercase();\n    for line in contents.lines() {\n        if line.to_lowercase().contains(&query) {\n            results.push(line);\n        }\n    }\n    results\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn case_sensitive() {\n        let query = \"duct\";\n        let contents = \"\\\nRust:\nsafe, fast, productive.\nPick three.\nDuct tape.\";\n\n        assert_eq!(vec![\"safe, fast, productive.\"], search(query, contents));\n    }\n\n    #[test]\n    fn case_insensitive() {\n        let query = \"rUsT\";\n        let contents = \"\\\nRust:\nsafe, fast, productive.\nPick three.\nTrust me.\";\n\n        assert_eq!(\n            vec![\"Rust:\", \"Trust me.\"],\n            search_case_insensitive(query, contents)\n        );\n    }\n}\n```\n\n`main.rs`\n\n:\n\n``` js\nuse std::env;\nuse std::process;\nuse minigrep::Config;\n\nfn main() {\n    let args: Vec<String> = env::args().collect();\n    let config = Config::new(&args).unwrap_or_else(|err| {\n        println!(\"Problem parsing arguments: {}\", err);\n        process::exit(1);\n    });\n    if let Err(e) = minigrep::run(config) {\n        println!(\"Application error: {}\", e);\n        process::exit(1);\n    }\n}\n```\n\n## 12.8.2 Standard Output vs. Standard Error\n\nThe current code prints all information, including error messages, to the terminal. Most terminals provide two output streams: **standard output ( stdout)** and\n\n**standard error (**.\n\n`stderr`\n\n)**General information should go to standard output, while error messages should go to standard error.** The advantage of this separation is that normal output can be redirected into a file while error messages still appear on the screen.\n\nThe `println!`\n\nmacro can only print to standard output. The `eprintln!`\n\nmacro can print to standard error.\n\nUsing the current code, run this command in the terminal:\n\n```\ncargo run > output.txt\n```\n\nThis redirects output to `output.txt`\n\n, but the command does not include any arguments, so the program should error. Because the error messages are also written to standard output, they end up in `output.txt`\n\n.\n\nA better approach is to print error messages to standard error, which keeps standard output clean and separate from errors.\n\n## 12.8.3 Modifying the Code\n\nChanging the code so that error messages go to standard error is quite simple. We only need to change all error printing from `println!`\n\nto `eprintln!`\n\n. Because all error handling is in `main.rs`\n\n, we only need a small change there, and `lib.rs`\n\ndoes not need to be modified at all:\n\n``` js\nuse std::env;\nuse std::process;\nuse minigrep::Config;\n\nfn main() {\n    let args: Vec<String> = env::args().collect();\n    let config = Config::new(&args).unwrap_or_else(|err| {\n        eprintln!(\"Problem parsing arguments: {}\", err);\n        process::exit(1);\n    });\n    if let Err(e) = minigrep::run(config) {\n        eprintln!(\"Application error: {}\", e);\n        process::exit(1);\n    }\n}\n```\n\nNow run the previous command again. It still has no arguments, so the program errors, but this time it will not put the error message into `output.txt`\n\n; instead, it will print directly in the terminal:\n\n``` bash\n$ cargo run > output.txt\nProblem parsing arguments: not enough arguments\n```\n\nThen try a normal run with arguments:\n\n``` bash\n$ cargo run -- to poem.txt > output.txt\n```\n\nThe output is redirected to `output.txt`\n\n. Open it:\n\n```\nAre you nobody, too?\nHow dreary to be somebody!\n```\n\nThat is the result we want: errors are printed directly in the terminal, while normal output is redirected into the file.", "url": "https://wpnews.pro/news/rust-guide-12-8-writing-error-messages-to-standard-error", "canonical_source": "https://dev.to/someb1oody/rust-guide128-writing-error-messages-to-standard-error-2jbp", "published_at": "2026-05-23 22:19:13+00:00", "updated_at": "2026-05-23 23:03:29.482245+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Rust", "grep"], "alternates": {"html": "https://wpnews.pro/news/rust-guide-12-8-writing-error-messages-to-standard-error", "markdown": "https://wpnews.pro/news/rust-guide-12-8-writing-error-messages-to-standard-error.md", "text": "https://wpnews.pro/news/rust-guide-12-8-writing-error-messages-to-standard-error.txt", "jsonld": "https://wpnews.pro/news/rust-guide-12-8-writing-error-messages-to-standard-error.jsonld"}}