Skip to content

Result

Transform and recover fallible values while preserving typed failures.

Updated View as Markdown

Result[T, E] represents the outcome of a fallible operation:

enum Result[T, E] {
    Ok(T)
    Err(E)
}

Function contracts can use the compact spelling T ! E. Its combinators are total and never panic.

Inspection

Method Result
is_ok() Whether the value is Ok
is_err() Whether the value is Err
is_ok_and(predicate) Test the success payload
is_err_and(predicate) Test the error payload
retry := response.is_err_and(error => error.is_retryable())

Inspection is appropriate for a boolean decision. Use match when either payload is needed:

match load_profile(id) {
    Ok(profile) => render(profile)
    Err(error) => report(error)
}

Fallbacks

value := operation.unwrap_or(default_value)
value := operation.unwrap_or_else(error => recover(error))

unwrap_or evaluates its default before the call. unwrap_or_else invokes its closure only for Err and supplies the error. Both discard the failure after producing a success value, so use them only when the fallback completely handles it.

The result-preserving alternatives are:

Method Success Failure
or(other) Keep the original success Use other
or_else(f) Keep the original success Call f(error)
and(other) Continue with other Preserve the original error

As with optional values, the closure form avoids doing fallback work on the success path.

Transformations

display := load()
    .map(value => value.to_string())
    .catch_err(error => UiError.LoadFailed { source: error })

map changes the success type. flat_map chains a fallible callback with the same error type. catch_err maps the error type and backs ordinary postfix catch lowering.

record := read_text(path)
    .flat_map(text => decode_record(text))
    .catch_err(error => ImportError.Invalid { source: error })

Call flatten on a nested Result[Result[T, E], E] to remove one result layer. If the inner and outer error types differ, map one error channel first.

Conversion

ok() preserves only the success as Option[T]; err() preserves only the failure. to_list() yields one success item or an empty list.

These conversions intentionally discard one channel. They are useful at an explicit boundary—for example, collecting only successful optional data—but should not replace propagation when the caller needs failure detail.

Control flow

Prefer language constructs when they expose the control flow more clearly:

Construct Purpose
value? Extract Ok or return the Err from the current function
match value Handle both variants locally
value catch pattern => expression Recover from or transform a failure
error value Exit the current fallible function with an error

Use methods for pipeline-shaped transformations. See Errors for the complete control-flow rules.

No panic unwrap

The prelude deliberately omits unwrap, expect, and error-unwrapping counterparts. Exhaustive matching and explicit fallback keep absence and failure visible in source.

to_string() includes the active variant and payload. hash_code() mixes the variant with the payload hash, allowing results to participate in derived hashes when their component types support the required operation.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close