Skip to content

std.result ​

Construction and unpacking of Result(T, E), and field access for the Error carrier.

yaoxiang
use std.result

Runtime Representation ​

ValueRepresentation
Result.ok(value)enum variant, carrying value
Result.err(error)enum variant, carrying error
Errorstruct, with fields (code, message)

Error.code is a registered code from the E6xxx / E7xxx range per RFC-013 (a stable contract across versions), and Error.message is a human-readable description.

Function Summary ​

FunctionSignature
is_ok(T: Type, E: Type)(self: &Result(T, E)) -> Bool
is_err(T: Type, E: Type)(self: &Result(T, E)) -> Bool
unwrap(T: Type, E: Type)(self: &Result(T, E)) -> T
unwrap_or(T: Type, E: Type)(self: &Result(T, E), default: T) -> T
ok(T: Type, E: Type)(value: T) -> Result(T, E)
err(T: Type, E: Type)(error: E) -> Result(T, E)
unwrap_err(T: Type, E: Type)(self: &Result(T, E)) -> E
code(self: &Error) -> String
message(self: &Error) -> String

Construction ​

ok ​

yaoxiang
ok: (T: Type, E: Type)(value: T) -> Result(T, E)

Wraps a success value.

The Ok value unpacked by ? must be re-wrapped before it can continue to propagate along a Result return type; ok is that wrapper.

yaoxiang
use std.assert
use std.result

main: () -> Void = {
    r = result.ok(42)
    assert(result.is_ok(r))
}

err ​

yaoxiang
err: (T: Type, E: Type)(error: E) -> Result(T, E)

Wraps an error value.

yaoxiang
use std.assert
use std.result

main: () -> Void = {
    r = result.err("boom")
    assert(result.is_err(r))
}

Predicates ​

is_ok ​

yaoxiang
is_ok: (T: Type, E: Type)(self: &Result(T, E)) -> Bool

Whether this is the success variant. Read-only borrow; self can be used repeatedly.

yaoxiang
use std.assert
use std.result

main: () -> Void = {
    r = result.ok(1)
    assert(result.is_ok(r))
    assert(result.is_ok(r))      // reusable
}

is_err ​

yaoxiang
is_err: (T: Type, E: Type)(self: &Result(T, E)) -> Bool

Whether this is the error variant. Read-only borrow.

yaoxiang
use std.assert
use std.result

main: () -> Void = {
    r = result.err("e")
    assert(result.is_err(r))
}

Extracting Values ​

unwrap ​

yaoxiang
unwrap: (T: Type, E: Type)(self: &Result(T, E)) -> T

Extracts the success value.

Returns: the value carried by the Ok variant. Error: calling on an Err value throws E6007, with the message including the original error code and description, in a form like unwrap called on Err value (E6010: parse_int: ...), so the failure reason is visible without first calling unwrap_err.

yaoxiang
use std.assert
use std.result
use std.string

main: () -> Void = {
    r = string.parse_int("42")
    assert(result.unwrap(r) == 42)
}

unwrap_or ​

yaoxiang
unwrap_or: (T: Type, E: Type)(self: &Result(T, E), default: T) -> T

Extracts the success value, or returns default on Err.

  • default —— fallback value when Err
yaoxiang
use std.assert
use std.result
use std.string

main: () -> Void = {
    good = string.parse_int("42")
    assert(result.unwrap_or(good, 0) == 42)

    bad = string.parse_int("abc")
    assert(result.unwrap_or(bad, 0) == 0)
}

unwrap_err ​

yaoxiang
unwrap_err: (T: Type, E: Type)(self: &Result(T, E)) -> E

Extracts the error value.

Returns: the value carried by the Err variant. Error: calling on an Ok value throws E6007.

yaoxiang
use std.assert
use std.result
use std.string

main: () -> Void = {
    r = string.parse_int("abc")
    e = result.unwrap_err(r)
    assert(result.is_err(r))
}

Error Fields ​

code ​

yaoxiang
code: (self: &Error) -> String

Reads the error code string, such as "E6010".

The signature type is Error, but the runtime error carrier is a struct with fields (code, message). Call directly on an Error value.

yaoxiang
use std.assert
use std.result
use std.string

main: () -> Void = {
    r = string.parse_int("abc")
    e = result.unwrap_err(r)
    assert(result.code(e) == "E6010")
}

message ​

yaoxiang
message: (self: &Error) -> String

Reads the error description text.

yaoxiang
use std.assert
use std.result
use std.string

main: () -> Void = {
    r = string.parse_int("abc")
    e = result.unwrap_err(r)
    assert(string.len(result.message(e)) > 0)
}