Skip to content

std/result

Import with import std/result.

type Infallible

Uninhabited type — no values can be constructed. Useful as the error type of a Result that cannot fail.

impl Debug

fn inspect(value: Infallible): String

impl Debug.inspect

enum Result<'T, 'E> {
    Ok 'T
    Err 'E
}

A value that is either a success (Ok) or a failure (Err).

Interactive Tests

parse = |s: String|
  if s == "" {
    Result.Err("empty")
  } else {
    Result.Ok(s)
  }
value = case parse("hi") {
  .Ok(v) -> v
  .Err(_) -> "?"
}
assert value == "hi"
fn ok?(result: Result<'T, 'E>): Bool

type function on Result

Returns True when the value is Ok.

Interactive Tests

assert Result.ok?(Result.Ok(3))
refute Result.ok?(Result.Err("bad"))
fn err?(result: Result<'T, 'E>): Bool

type function on Result

Returns True when the value is Err.

Interactive Tests

refute Result.err?(Result.Ok(3))
assert Result.err?(Result.Err("bad"))
fn map(result: Result<'T, 'E>, f: ('T) -> 'U): Result<'U, 'E>

type function on Result

Transforms the Ok value, passes Err through.

Interactive Tests

assert Result.map(Result.Ok(3), |x| x * 2) == Result.Ok(6)
assert Result.map(Result.Err("bad"), |x| x * 2) == Result.Err("bad")
fn with_default(result: Result<'T, 'E>, default: 'T): 'T

type function on Result

Returns the Ok value, or the default if Err.

Interactive Tests

assert Result.with_default(Result.Ok(5), 0) == 5
assert Result.with_default(Result.Err("bad"), 0) == 0
fn flat_map(result: Result<'T, 'E>, f: ('T) -> Result<'U, 'E>): Result<'U, 'E>

type function on Result

Like map, but the function returns a Result. Flattens the result.

Interactive Tests

parse_int = |s: String|
  if s == "1" {
    Result.Ok(1)
  } else {
    Result.Err("nope")
  }
assert Result.flat_map(Result.Ok("1"), |s| parse_int(s)) == Result.Ok(1)
assert Result.flat_map(Result.Ok("x"), |s| parse_int(s)) == Result.Err("nope")
fn map_err(result: Result<'T, 'E>, f: ('E) -> 'F): Result<'T, 'F>

type function on Result

Transforms the Err value, passes Ok through.

Interactive Tests

assert Result.map_err(Result.Err("bad"), |e: String|
  "error: " + e
) == Result.Err("error: bad")
assert Result.map_err(Result.Ok(3), |e: String| "error: " + e) == Result.Ok(3)
fn to_maybe(result: Result<'T, 'E>): Maybe<'T>

type function on Result

Converts Result to Maybe. Err becomes None; the error value is discarded.

Interactive Tests

assert Result.to_maybe(Result.Ok(3)) == Some(3)
assert Result.to_maybe(Result.Err("bad")) == None
fn from_maybe(maybe: Maybe<'T>, error: 'E): Result<'T, 'E>

type function on Result

Converts Maybe to Result. None becomes Err(error).

Interactive Tests

assert Result.from_maybe(Some(3), "missing") == Result.Ok(3)
assert Result.from_maybe(None, "missing") == Result.Err("missing")

impl Display

fn to_string<'T, 'E>(value: Result<'T, 'E>): String where 'T: Display, 'E: Display

impl Display.to_string

impl Equatable

fn equal?<'T, 'E>(a: Result<'T, 'E>, b: Result<'T, 'E>): Bool where 'T: Equatable, 'E: Equatable

impl Equatable.equal?

impl Hashable

fn hash<'T, 'E>(value: Result<'T, 'E>): Int where 'T: Hashable, 'E: Hashable

impl Hashable.hash

impl Debug

fn inspect<'T, 'E>(value: Result<'T, 'E>): String where 'T: Debug, 'E: Debug

impl Debug.inspect