Skip to main content

error handling

option

Option[T] represents a value that may or may not be present.

Option[T] = Some(T) | None
fn find(arr: Array[str], val: str) Option[str] {
// ...
}

let result = find(arr, "hello");
match result {
Some(v) => stdout.println("found: {}", v),
None => stdout.println("not found"),
}

result

Result[T, E] represents either a success value or an error.

Result[T, E] = Ok(T) | Err(E)
fn read_file(path: str) Result[str, str] {
// ...
}

match read_file("config.void") {
Ok(content) => stdout.println("got: {}", content),
Err(e) => stdout.println("error: {}", e),
}

the ? operator

? propagates errors up the call stack automatically. if the value is Err or None, the function returns early with that value.

fn load_config() Result[Config, str] {
let file = open_file("config.void")?;
let parsed = parse(file)?;
Ok(parsed)
}

custom ? propagation

any type can work with ? by implementing the Propagatable trait:

trait Propagatable[T, E] {
fn is_ok(self) bool;
fn unwrap(self) T;
fn propagate(self) E;
}

Result and Option implement this trait in stdlib. you can implement it for your own types too.