08 · Error Handling
Expression Result recovery
recover evaluates its Result once and unwraps Ok. Its patterns match the Err payload and must cover that payload type. Continuing branches return the same type as the successful value. Returns, propagation and cleanup still belong to the surrounding scope.
On this page
recover evaluates its Result once and unwraps Ok. Its patterns match the Err payload and must cover that payload type. Continuing branches return the same type as the successful value. Returns, propagation and cleanup still belong to the surrounding scope.
Complete example
Expression Result recovery.
recover_expression.zolo
// A successful Result keeps its value. Recovery handles only its error.
enum ReadError { Missing, Invalid }
fn number(result: Result<int, ReadError>) -> int {
result recover {
.Missing => 0,
.Invalid => -1,
}
}
fn preserve(result: Result<int, str>) -> Result<int, str> {
let value = result recover { error => return Result.Err(error) }
Result.Ok(value)
}
fn main() {
print(number(Result.Ok(7)))
print(number(Result.Err(ReadError.Missing)))
print(number(Result.Err(ReadError.Invalid)))
print(preserve(Result.Err("missing")).is_err())
}
Rules and behavior
Conditions and error recovery · Documentation
From a checkout of the repository:
zolo examples/features/recover_expression.zolo
See also