Errors and user-facing diagnostics
Quick answer
A useful diagnostic explains what failed and how the user can correct it, without exposing credentials or overwhelming them with internal details.
Translate errors at the interface
Keep typed errors inside the application; at the CLI boundary map them to concise human messages and documented exit codes. A file-not-found condition differs from permission denial. Avoid dumping secrets, complete request headers or internal stack traces. Preserve an error source where needed for controlled diagnostics, while keeping normal stdout clean for pipelines.
use std::io::ErrorKind;fn advice(k: ErrorKind) -> &'static str { match k { ErrorKind::NotFound => "check the path", ErrorKind::PermissionDenied => "check access rights", _ => "I/O failed" }}fn main() { println!("{}", advice(ErrorKind::NotFound)); println!("{}", advice(ErrorKind::PermissionDenied)); }Expected stdout (not executed):
check the pathcheck access rightsSpecific guidance helps without changing the underlying failure.
use std::process::ExitCode;fn main() -> ExitCode { let result: Result<u32, &str> = Err("count must be positive"); match result { Ok(n) => { println!("{n}"); ExitCode::SUCCESS }, Err(e) => { eprintln!("error: {e}"); ExitCode::FAILURE } }}Manual local Rust/Cargo candidate; not executed. Expected behavior:
Empty stdout, stderr error: count must be positive, failure exit status.A pipeline consuming stdout will not read the error as data.
Common mistake
Never print an access token just to explain an authentication error.
Try it yourself
Add a diagnostic for invalid UTF-8 and a test for its wording.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Errors and user-facing diagnostics — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A file cannot be opened because of permissions. Which response is useful?
Errors and user-facing diagnostics — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::io::ErrorKind; fn advice(k: ErrorKind) -> &'static str { match k { ErrorKind::NotFound => "check the path", ErrorKind::PermissionDenied => "check access rights", _ => "I/O failed" } } fn main() { println!("{}", advice(ErrorKind::NotFound)); println!("{}", advice(ErrorKind::PermissionDenied)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Errors and user-facing diagnostics — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::process::ExitCode; fn main() -> ExitCode { let result: Result<u32, &str> = Err("count must be positive"); match result { Ok(n) => { println!("{n}"); ExitCode::SUCCESS }, Err(e) => { eprintln!("error: {e}"); ExitCode::FAILURE } } } ``` Which outcome or interpretation best matches this example?