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),}Result.is_ok()
Section titled “Result.is_ok()”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 parsedResult.is_err()
Section titled “Result.is_err()”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 parsedResult.ok()
Section titled “Result.ok()”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 parsedResult.err()
Section titled “Result.err()”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 parsedResult.unwrap_or()
Section titled “Result.unwrap_or()”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 80Result.unwrap_or_else()
Section titled “Result.unwrap_or_else()”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})Result.map()
Section titled “Result.map()”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 parsedResult.map_err()
Section titled “Result.map_err()”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) untouchedResult.map_or()
Section titled “Result.map_or()”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 0Result.map_or_else()
Section titled “Result.map_or_else()”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,)Result.and_then()
Section titled “Result.and_then()”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")) }})Result.or_else()
Section titled “Result.or_else()”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 parseResult.wrap_err()
Section titled “Result.wrap_err()”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