Skip to content

Result Reference

A Result represents either success (Ok) or failure (Err), used for error handling without exceptions.

Result is an abstract base class with two concrete subclasses:

  • Ok - represents a successful value
  • Err- represents an error value
var success = Ok(42);
var failure = Err("Something went wrong");

Returns the error value if Err, otherwise returns nil.

var result = Err("Something went wrong");
println(result.err()); // "Something went wrong"
var success = Ok(42);
println(success.err()); // nil

None

dyn! - The error value or nil

Returns the success value if Ok, otherwise panics with a custom message.

var result = Ok(42);
var value = result.expect("Expected a valid number"); // 42
var error = Err("original error");
// var value = error.expect("Custom error message"); // Panics with "Custom error message"
  • message: String - The error message to display if the result is Err

dyn! - The success value

Throws with the provided message if the result is Err

Transforms the success value with a function that returns a Result, otherwise returns the error unchanged.

var result = Ok(5);
var new_result = result.flat_map((x) -> Ok(x * 2)); // Ok(10)
var error = Err("error");
var still_error = error.flat_map((x) -> Ok(x * 2)); // Err("error")
  • transform: (dyn!) -> Result - A function that takes the success value and returns a new Result

Result - The result from the transform function, or the original error

Calls the provided function if the Result is Err.

var result = Err("error");
var maybe_value = result.if_err((err) -> println(err));

callback: (dyn!) -> nil - A callback that will be called if the Result is Err.

Result - The instance of the Result object.

Calls the provided function if the Result is Ok.

var result = Ok("Message");
var maybe_value = result.if_ok((message) -> println(message));

callback: (dyn!) -> nil - A callback that will be called if the Result is Ok.

Result - The instance of the Result object.

Returns true if the result is Err.

var result = Err("error");
println(result.is_err()); // true

None

Boolean - true if the result is Err, false otherwise

Returns true if the result is Ok.

var result = Ok(42);
println(result.is_ok()); // true

None

Boolean - true if the result is Ok, false otherwise

Transforms the success value if Ok, otherwise returns the error unchanged.

var result = Ok(5);
var doubled = result.map((x) -> x * 2); // Ok(10)
var error = Err("error");
var still_error = error.map((x) -> x * 2); // Err("error")
  • transform: (dyn!) -> dyn! - A function that transforms the success value

Result - A new Result with the transformed value, or the original error

Transforms the error value if Err, otherwise returns the success unchanged.

var result = Ok(42);
var still_ok = result.map_err((err) -> "New " + err); // Ok(42)
var error = Err("error");
var new_error = error.map_err((err) -> "New " + err); // Err("New error")
  • transform: (dyn!) -> Err - A function that transforms the error value

Result - The original success, or a new Result with the transformed error

Returns the success value if Ok, otherwise returns nil.

var result = Ok(42);
println(result.ok()); // 42
var error = Err("error");
println(error.ok()); // nil

None

dyn! - The success value or nil

Returns the current result if Ok, otherwise calls the provided function and returns its result.

var result = Ok(42);
var new_result = result.or_else(() -> Ok(0)); // Ok(42)
var error = Err("error");
var new_result = error.or_else(() -> Ok(0)); // Ok(0)
  • callback: () -> Result - A function that returns a new Result

Result - The current result if Ok, or the result from the callback

Returns the success value if Ok, otherwise panics with the error value.

var result = Ok(42);
var value = result.unwrap(); // 42
var error = Err("error");
// var value = error.unwrap(); // Panics with "error"!

None

dyn! - The success value

Throws if the result is Err, displaying the error value

Returns the success value if Ok, otherwise returns the provided default value.

var result = Ok(42);
var value = result.unwrap_or(0); // 42
var error = Err("error");
var value = error.unwrap_or(0); // 0
  • default_value: dyn! - The value to return if the result is Err

dyn! - The success value or the default value

Returns the success value if Ok, otherwise calls the provided function and returns its result.

var result = Ok(42);
var value = result.unwrap_or_else(() -> 0); // 42
var error = Err("error");
var value = error.unwrap_or_else(() -> 0); // 0
  • callback: () -> dyn! - A function that returns a default value

dyn! - The success value or the result of calling the callback