error-concept
Domain errors: distinguish business rules
Represent invalid business input separately from infrastructure failures. A caller can choose a specific corrective message. Expected stdout (not executed): Err(Empty) Ok(3) Expected stderr: (empty) Expected exit: success Common mistake: Reporting a business rule violation as a panic or retrying an unchanged invalid request.
Formal Syntax & Structure
rust syntaxrust
enum OrderError { Empty, TooMany }Working Example
Example Snippetrust
#[derive(Debug, PartialEq)]enum OrderError { Empty, TooMany }fn order(n: u32) -> Result<u32, OrderError> { match n { 0 => Err(OrderError::Empty), 1..=10 => Ok(n), _ => Err(OrderError::TooMany) }}fn main() { println!("{:?}", order(0)); println!("{:?}", order(3)); }