Tin

Examples · learn/07-errors · 131 of 339

result

Result<T, E> is Ok(value) or Err(error): an operation that can fail, and why.

examples/learn/07-errors/result.tintin run examples/learn/07-errors/result.tin
// Result<T, E> is Ok(value) or Err(error): an operation that can fail, and why.
fn divide(a: int, b: int): Result<int, string> {
  if b == 0 { return Err("cannot divide {a} by zero") }
  Ok(a / b)
}

fn main() {
  print(divide(10, 2))
  print(divide(1, 0))
  match divide(7, 0) {
    Ok(n) => print("result: {n}")
    Err(why) => print("failed: {why}")
  }
}