-
Notifications
You must be signed in to change notification settings - Fork 0
Result
maslina524 edited this page Apr 20, 2026
·
2 revisions
Error handling with the Result type.
Result[T, E] is the type used for returning and propagating errors.
In addition to working with pattern matching, Result provides a wide variety of different methods.
-
okreturnsResultwith the value inOk -
errreturnsResultwith the value inErr
The is_ok and is_err methods return True if the Result is Ok or Err, respectively.
These methods extract the contained value in a Result<T, E> when it is the Ok variant. If the Result is Err:
-
expectpanics with a provided custom message -
unwrappanics with a generic message -
unwrap_orreturns the provided default value -
unwrap_or_elsereturns the result of evaluating the provided function
This method work the other way around if Result<T, E> is the Err variant:
-
unwrap_errpanics with a generic message
-
maptransformsResult<T, E>intoResult<U, E>by applying the provided function to the contained value ofOkand leavingErrvalues unchanged -
map_errtransformsResult<T, E>intoResult<T, U>by applying the provided function to the contained value ofErrand leavingOkvalues unchanged -
map_orapplies the provided function to the contained value ofOk, or returns the provided default value if the Result isErr -
map_or_elseapplies the provided function to the contained value ofOk, or applies the provided default fallback function to the contained value ofErr
Creates a Result<T, E> with the Ok and Err variants and safely extracts the value:
from rustify import Result
ok = Result.ok("Hello World")
err = Result.err("I'm a error")
print(f"{ok.unwrap()}") # Hello World
print(f"{err.unwrap_or("Unwrap Err")}") # Unwrap Err