std.result

Standard-library module std.result.

Importuse std.result

Functions

fn map<T, U>(result: Result<T, Error>, f: fn(T) -> U) -> Result<U, Error>#

Applies f to a success value; an error passes through unchanged.

  • result: The result to map.
  • f: Applied to the success value.
let r: Result<int, Error> = ok(2)
println(r.map(|x| x * 10).unwrap()) // 20
fn map_err<T>(result: Result<T, Error>, f: fn(Error) -> Error) -> Result<T, Error>#

Applies f to the error; a success passes through unchanged.

  • result: The result to map.
  • f: Applied to the error, producing the error to carry instead.
let r: Result<int, Error> = err.Io("disk")
println(r.map_err(|e| err.Wrapped("while saving: " + e.message).unwrap_err()).unwrap_err().name) // Wrapped
fn map<T, U>(result: Result<T, Error>, f: fn(T) -> U) -> Result<U, Error>#

Applies f to a success value; an error passes through unchanged.

  • result: The result to map.
  • f: Applied to the success value.
let r: Result<int, Error> = ok(2)
println(r.map(|x| x * 10).unwrap()) // 20
fn or_else<T>(result: Result<T, Error>, f: fn(Error) -> Result<T, Error>) -> Result<T, Error>#

Recovers from an error with f; a success passes through unchanged.

  • result: The result to recover.
  • f: Applied to the error; may succeed or fail again.
fn unwrap_or_else<T>(result: Result<T, Error>, f: fn(Error) -> T) -> T#

The success value, otherwise the value f computes from the error.

  • result: The result to unwrap.
  • f: Called only on an error.
let r: Result<int, Error> = err.Missing("gone")
println(r.unwrap_or_else(|e| e.message.len())) // 4
fn unwrap_err<T>(result: Result<T, Error>) -> Error#

The error of a failed result; stops the program on a success.

  • result: The result expected to have failed.
fn ok<T>(result: Result<T, Error>) -> Optional<T>#

The success value as an optional; an error becomes absence.

  • result: The result to convert.
let r: Result<int, Error> = ok(1)
println(r.ok()) // some(1)
fn err<T>(result: Result<T, Error>) -> Optional<Error>#

The error as an optional; a success becomes absence.

  • result: The result to convert.