Skip to content

Result

A result that either succeeded or failed. Equivalent to Go’s (T, error), or to a bare error when there is no value to carry.

enum Result<T, E> {
Ok(T),
Err(E),
}

Returns true if Result is Ok.

impl<T, E> Result<T, E> {
fn is_ok(self) -> bool
}
let raw = "8080"
let succeeded = strconv.Atoi(raw).is_ok()
true if raw parsed

Returns true if Result is Err.

impl<T, E> Result<T, E> {
fn is_err(self) -> bool
}
let raw = "8080"
let failed = strconv.Atoi(raw).is_err()
false if raw parsed

Converts to Option<T>, discarding the error if any.

impl<T, E> Result<T, E> {
fn ok(self) -> Option<T>
}
let raw = "8080"
let maybe_port = strconv.Atoi(raw).ok()
Some(8080) if raw parsed

Converts to Option<E>, discarding the success value if any.

impl<T, E> Result<T, E> {
fn err(self) -> Option<E>
}
let raw = "8080"
let maybe_failure = strconv.Atoi(raw).err()
None if raw parsed

Returns the wrapped Ok value, or default if Err.

impl<T, E> Result<T, E> {
fn unwrap_or(self, default: T) -> T
}
let raw = "8080"
let port = strconv.Atoi(raw).unwrap_or(80)
8080 if raw parsed, else 80

Returns the wrapped Ok value, or computes it by calling f with the Err value.

impl<T, E> Result<T, E> {
fn unwrap_or_else(self, f: fn(E) -> T) -> T
}
let raw = "8080"
let port = strconv.Atoi(raw).unwrap_or_else(|failure| {
runs f only if raw failed to parse
fmt.Println(failure.Error())
80
})

Transforms the Ok value by applying f, leaving Err unchanged.

impl<T, E> Result<T, E> {
fn map<U>(self, f: fn(T) -> U) -> Result<U, E>
}
let raw = "8080"
let doubled = strconv.Atoi(raw).map(|n| n * 2)
Ok(16160) if raw parsed

Transforms the Err value by applying f, leaving Ok unchanged.

impl<T, E> Result<T, E> {
fn map_err<F>(self, f: fn(E) -> F) -> Result<T, F>
}
let raw = "8080"
let described = strconv.Atoi(raw).map_err(|failure| failure.Error())
leaves Ok(8080) untouched

Returns default if Err, otherwise applies f to the Ok value.

impl<T, E> Result<T, E> {
fn map_or<U>(self, default: U, f: fn(T) -> U) -> U
}
let raw = "8080"
let doubled = strconv.Atoi(raw).map_or(0, |n| n * 2)
16160 if raw parsed, else 0

Applies default to the Err value, or f to the Ok value.

impl<T, E> Result<T, E> {
fn map_or_else<U>(self, default: fn(E) -> U, f: fn(T) -> U) -> U
}
let raw = "8080"
let shown = strconv.Atoi(raw).map_or_else(
port, or parse error
|failure| failure.Error(),
strconv.Itoa,
)

Calls f with the Ok value and returns the result, leaving Err unchanged.

impl<T, E> Result<T, E> {
fn and_then<U>(self, f: fn(T) -> Result<U, E>) -> Result<U, E>
}
let raw = "8080"
let port = strconv.Atoi(raw).and_then(|n| {
a second check, run only if raw parsed
if n <= 65535 { Ok(n) } else { Err(errors.New("port out of range")) }
})

Calls f with the Err value and returns the result, leaving Ok unchanged.

impl<T, E> Result<T, E> {
fn or_else<F>(self, f: fn(E) -> Result<T, F>) -> Result<T, F>
}
let raw = "8080"
let port = strconv.Atoi(raw).or_else(|_| strconv.Atoi("80"))
runs f only if raw failed to parse

Wraps the Err value with msg and the original error, leaving Ok unchanged.

impl<T> Result<T, error> {
fn wrap_err(self, msg: string) -> Result<T, error>
}
let port = strconv.Atoi(raw).wrap_err("read port")
Err("read port: ..."), keeping the original