std/result
Import with import std/result.
Exports
Section titled “Exports”Types
type Infallible
Section titled “type Infallible”type Infallible
Uninhabited type — no values can be constructed. Useful as the error type
of a Result that cannot fail.
Infallible.inspect
Section titled “Infallible.inspect”impl Debug
fn inspect(value: Infallible): String
impl Debug.inspect
enum Result
Section titled “enum Result”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"
Result.ok?
Section titled “Result.ok?”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"))
Result.err?
Section titled “Result.err?”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"))
Result.map
Section titled “Result.map”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")
Result.with_default
Section titled “Result.with_default”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
Result.flat_map
Section titled “Result.flat_map”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")
Result.map_err
Section titled “Result.map_err”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)
Result.to_maybe
Section titled “Result.to_maybe”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
Result.from_maybe
Section titled “Result.from_maybe”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")
Result.to_string
Section titled “Result.to_string”impl Display
fn to_string<'T, 'E>(value: Result<'T, 'E>): String where 'T: Display, 'E: Display
impl Display.to_string
Result.equal?
Section titled “Result.equal?”impl Equatable
fn equal?<'T, 'E>(a: Result<'T, 'E>, b: Result<'T, 'E>): Bool where 'T: Equatable, 'E: Equatable
impl Equatable.equal?
Result.hash
Section titled “Result.hash”impl Hashable
fn hash<'T, 'E>(value: Result<'T, 'E>): Int where 'T: Hashable, 'E: Hashable
impl Hashable.hash
Result.inspect
Section titled “Result.inspect”impl Debug
fn inspect<'T, 'E>(value: Result<'T, 'E>): String where 'T: Debug, 'E: Debug
impl Debug.inspect