API reference

Every declaration below is extracted from zig-error-handling's source, with the doc comments as written there. A declaration listed without prose is public but undocumented in the source.

Result

fn Result(comptime T: type, comptime E: type) type

Result type similar to Rust's Result<T, E> and TypeScript's neverthrow Represents either a success value (Ok) or an error value (Err)

ok

fn ok(value: T) Self

Create a successful Result containing a value

err

fn err(error_value: E) Self

Create an error Result containing an error

isOk

fn isOk(self: Self) bool

Returns true if the Result is Ok

isErr

fn isErr(self: Self) bool

Returns true if the Result is Err

map

fn map(self: Self, comptime U: type, func: fn (T) U) Result(U, E)

Maps a Result<T, E> to Result<U, E> by applying a function to the Ok value

mapErr

fn mapErr(self: Self, comptime F: type, func: fn (E) F) Result(T, F)

Maps a Result<T, E> to Result<T, F> by applying a function to the Err value

mapBoth

fn mapBoth(self: Self, comptime U: type, okFunc: fn (T) U, errFunc: fn (E) U) U

Maps both Ok and Err values, similar to Rust's map_or_else

andThen

fn andThen(self: Self, comptime U: type, func: fn (T) Result(U, E)) Result(U, E)

Chains Results together (flatMap/andThen) Calls func with the Ok value if Ok, otherwise returns the Err

orElse

fn orElse(self: Self, func: fn (E) Self) Self

Chains error handling (orElse) Calls func with the Err value if Err, otherwise returns the Ok

unwrapOr

fn unwrapOr(self: Self, default: T) T

Returns the Ok value or a default value

unwrapOrElse

fn unwrapOrElse(self: Self, func: fn (E) T) T

Returns the Ok value or computes it from the error using a function

unwrap

fn unwrap(self: Self) T

Returns the Ok value or panics with a message

unwrapErr

fn unwrapErr(self: Self) E

Returns the Err value or panics with a message

expect

fn expect(self: Self, msg: []const u8) T

Returns the Ok value or panics with a custom message

expectErr

fn expectErr(self: Self, msg: []const u8) E

Returns the Err value or panics with a custom message

match

fn match(self: Self, comptime U: type, handlers: struct

Pattern matching on Result - similar to Rust's match

combine

fn combine(self: Self, other: Self) Result(struct

Combines two Results - if both are Ok, returns Ok with both values as a tuple

orResult

fn orResult(self: Self, other: Self) Self

Returns the Ok value if present, otherwise returns the other Result

andResult

fn andResult(self: Self, other: Self) Self

Returns self if it's an error, otherwise returns the other Result

okOrNull

fn okOrNull(self: Self) ?T

Converts to an optional, discarding the error

errOrNull

fn errOrNull(self: Self) ?E

Converts to an optional error, discarding the ok value

inspect

fn inspect(self: Self, func: fn (T) void) Self

Inspect the Ok value without consuming or transforming it Useful for debugging or logging

inspectErr

fn inspectErr(self: Self, func: fn (E) void) Self

Inspect the Err value without consuming or transforming it

flatten

fn flatten(self: Result(Self, E)) Self

Flatten a nested Result - Result(Result(T, E), E) -> Result(T, E)

toErrorUnion

fn toErrorUnion(self: Self) E!T

Returns the Ok value as an error union, or the Err as an error This allows safe unwrapping that returns a Zig error union

ok

fn ok(value: anytype) Result(@TypeOf(value), anyerror)

Helper to create Ok results

err

fn err(comptime T: type, error_value: anytype) Result(T, @TypeOf(error_value))

Helper to create Err results with type inference

fromErrorUnion

fn fromErrorUnion(value: anytype) Result(

Convert a Zig error union to a Result

transpose

fn transpose(comptime T: type, comptime E: type, result: Result(?T, E)) ?Result(T, E)

Transpose Result<?T, E> to ?Result(T, E) If Ok(null), returns null. If Ok(some), returns Some(Ok(some)). If Err, returns Some(Err)

collect

fn collect(

Collect a slice of Results into a Result of a slice If any Result is Err, returns the first Err. Otherwise returns Ok with all values. The caller owns the returned slice and must free it with the allocator. Note: For error types that aren't error sets, OOM cannot be represented

partition

fn partition(

Partition a slice of Results into separate Ok and Err slices Returns a tuple of (ok_values, err_values) Caller owns both slices and must free them.

sequence

fn sequence(

Sequence a slice of Results, succeeding only if all succeed Similar to collect but more efficient as it stops at first error