error-concept

CLI error boundaries and ExitCode

Keep machine-readable success output separate from human-readable failure messages. Return an exit status at the outermost boundary so callers can detect failure. This fixture intentionally exits unsuccessfully, but does not panic. Expected stdout (not executed): (empty) Expected stderr: invalid count Expected exit: failure Common mistake: Printing an error and returning success, or assuming a portable numeric failure code.

Formal Syntax & Structure

rust syntaxrust
fn main() -> std::process::ExitCode

Working Example

Example Snippetrust
use std::process::ExitCode;fn main() -> ExitCode {    match "bad".parse::<u32>() {        Ok(n) => { println!("{n}"); ExitCode::SUCCESS }        Err(_) => { eprintln!("invalid count"); ExitCode::FAILURE }    }}
Rust Reference