Result¶
Result models the explicit outcome of an operation. It represents either success (Ok) or failure (Err), without relying on exceptions for control flow.
Result is about flow, not about error representation.
When to use¶
Use Result when failure is part of normal behavior — validation, parsing, rule evaluation, boundary checks. Return Ok(value) for success, Err(error) for failure.
Variants¶
- Ok[ValueType] — Wraps a successful value.
is_okisTrue,is_errisFalse. - Err[ErrorType] — Wraps an error.
is_okisFalse,is_errisTrue.
Both support structural pattern matching through __match_args__.
Common operations¶
is_ok/is_err— Branch on the variant (property, not method)value— Unwrap the success payloaderror— Unwrap the error payloadmap(fn)— Transform success, pass errors throughmap_error(fn)— Transform error, pass success throughflat_map(fn)— Chain operations that returnResultget_value_or(default)— Fallback on errorget_value_or_else(fn)— Compute fallback from the error
Design¶
Result is a Protocol — Ok and Err are concrete implementations. You construct Ok(...) or Err(...) but type-annotate with Result[ValueType, ErrorType].
Related
See Errors for the Error type used as the error payload in Err.