result
The result module is auto-imported — no import statement needed.
Functions
Section titled “Functions”| Function | Signature | Description |
|---|---|---|
map | (Result[A, E], (A) -> B) -> Result[B, E] | Transform the ok value using a function. If err, passes through unchanged. |
map_err | (Result[A, E], (E) -> F) -> Result[A, F] | Transform the err value using a function. If ok, passes through unchanged. |
flat_map | (Result[A, E], (A) -> Result[B, E]) -> Result[B, E] | Chain a Result-returning function on the ok value. Flattens nested Results. |
unwrap_or | (Result[A, E], A) -> A | Get the ok value, or return a default if err. |
unwrap_or_else | (Result[A, E], (E) -> A) -> A | Get the ok value, or compute a default from the error using a function. |
is_ok | (Result[A, E]) -> Bool | Check if the Result is ok. |
is_err | (Result[A, E]) -> Bool | Check if the Result is err. |
to_option | (Result[A, E]) -> Option[A] | Convert ok to some, err to none. Discards the error value. |
to_err_option | (Result[A, E]) -> Option[E] | Convert err to some, ok to none. Discards the ok value. |
collect | (List[Result[T, E]]) -> Result[List[T], List[E]] | 非推奨(E039・ADR-0007、削除予定) — partition を使う。 |
partition | (List[Result[T, E]]) -> (List[T], List[E]) | Partition a list of Results into ok values and err values. |
collect_map | (List[T], (T) -> Result[U, E]) -> Result[List[U], List[E]] | 非推奨(E039・ADR-0007、削除予定) — partition を使う。 |
Reference
Section titled “Reference”result.map(r: Result[A, E], f: (A) -> B) -> Result[B, E]
Section titled “result.map(r: Result[A, E], f: (A) -> B) -> Result[B, E]”Transform the ok value using a function. If err, passes through unchanged.
result.map(ok(2), (x) => x * 10)result.map_err(r: Result[A, E], f: (E) -> F) -> Result[A, F]
Section titled “result.map_err(r: Result[A, E], f: (E) -> F) -> Result[A, F]”Transform the err value using a function. If ok, passes through unchanged.
result.map_err(err("fail"), (e) => "wrapped: " ++ e)result.flat_map(r: Result[A, E], f: (A) -> Result[B, E]) -> Result[B, E]
Section titled “result.flat_map(r: Result[A, E], f: (A) -> Result[B, E]) -> Result[B, E]”Chain a Result-returning function on the ok value. Flattens nested Results.
result.flat_map(ok(5), (x) => if x > 0 then ok(x) else err("negative"))result.unwrap_or(r: Result[A, E], default: A) -> A
Section titled “result.unwrap_or(r: Result[A, E], default: A) -> A”Get the ok value, or return a default if err.
result.unwrap_or(err("fail"), 0)result.unwrap_or_else(r: Result[A, E], f: (E) -> A) -> A
Section titled “result.unwrap_or_else(r: Result[A, E], f: (E) -> A) -> A”Get the ok value, or compute a default from the error using a function.
result.unwrap_or_else(err("fail"), (e) => string.len(e))result.is_ok(r: Result[A, E]) -> Bool
Section titled “result.is_ok(r: Result[A, E]) -> Bool”Check if the Result is ok.
result.is_ok(ok(42))result.is_err(r: Result[A, E]) -> Bool
Section titled “result.is_err(r: Result[A, E]) -> Bool”Check if the Result is err.
result.is_err(err("fail"))result.to_option(r: Result[A, E]) -> Option[A]
Section titled “result.to_option(r: Result[A, E]) -> Option[A]”Convert ok to some, err to none. Discards the error value.
result.to_option(ok(42))result.to_err_option(r: Result[A, E]) -> Option[E]
Section titled “result.to_err_option(r: Result[A, E]) -> Option[E]”Convert err to some, ok to none. Discards the ok value.
result.to_err_option(err("fail"))result.collect(rs: List[Result[T, E]]) -> Result[List[T], List[E]]
Section titled “result.collect(rs: List[Result[T, E]]) -> Result[List[T], List[E]]”非推奨(E039・ADR-0007): Rust の
collectは最初の Err で打ち切る(E は E のまま)のに対し、この関数は全エラーを集める — 同じ名前で逆の戦略。名前ごと引退し、全エラー収集はpartitionで綴る:let (oks, errs) = result.partition(rs)if list.is_empty(errs) then ok(oks) else err(errs)
Collect a list of Results. All ok → ok(values), any err → err(all_errors).
result.collect([ok(1), ok(2), ok(3)]) // => ok([1, 2, 3])result.partition(rs: List[Result[T, E]]) -> (List[T], List[E])
Section titled “result.partition(rs: List[Result[T, E]]) -> (List[T], List[E])”Partition a list of Results into ok values and err values.
result.partition([ok(1), err("x"), ok(2)]) // => ([1, 2], ["x"])result.collect_map(xs: List[T], f: (T) -> Result[U, E]) -> Result[List[U], List[E]]
Section titled “result.collect_map(xs: List[T], f: (T) -> Result[U, E]) -> Result[List[U], List[E]]”Map a function over a list and collect Results. All ok → ok(values), any err → err(all_errors).
result.collect_map([1, 2, 3], (x) => if x > 0 then ok(x) else err("neg"))