std.result.Result
class pub enum Result[T, E]
A type that represents either success (Ok(T)
) or failure (Error(E)
).
Constructors
Ok
Ok(T)
The case and value for a successful result.
Error
Error(E)
The case and value for an error.
Static methods
collect
Show source codeHide source code
fn pub static collect[I: mut + Iter[Result[T, E]]](
iter: I,
) -> Result[Array[T], E] {
let vals = []
loop {
match iter.next {
case Some(Ok(val)) -> vals.push(val)
case Some(Error(err)) -> throw err
case _ -> break
}
}
Result.Ok(vals)
}
fn pub static collect[I: mut + Iter[Result[T, E]]](iter: I: mut) -> Result[Array[T], E]
Collects values from an Iter[Result[T, E]]
into a Result[Array[T], E]
,
returning the first Error
encountered when iterating over the iterator.
Examples
let vals = [Result.Ok(1), Result.Error('oops!'), Result.Ok(2)].into_iter
let result = Result.collect(vals)
result.error? # => true
Instance methods
!=
Show source codeHide source code
fn pub !=(other: T) -> Bool {
(self == other).false?
}
fn pub !=(other: T) -> Bool
Returns true
if self
and the given object are not equal to each other.
==
Show source codeHide source code
fn pub ==(other: ref Result[T, E]) -> Bool {
match self {
case Ok(ours) -> {
match other {
case Ok(theirs) -> ours == theirs
case _ -> false
}
}
case Error(ours) -> {
match other {
case Error(theirs) -> ours == theirs
case _ -> false
}
}
}
}
fn pub ==(other: ref Result[T, E]) -> Bool
if
E: Equal[ref E],
T: Equal[ref T]
Returns true
if self
and the given object are equal to each other.
This operator is used to perform structural equality. This means two objects residing in different memory locations may be considered equal, provided their structure is equal. For example, two different arrays may be considered to have structural equality if they contain the exact same values.
clone
Show source codeHide source code
fn pub clone -> Result[T, E] {
match self {
case Ok(v) -> Result.Ok(v.clone)
case Error(v) -> Result.Error(v.clone)
}
}
fn pub clone -> Result[T, E]
if
E: Clone[E],
T: Clone[T]
Creates a clone of self
.
else
Show source codeHide source code
fn pub move else[R](block: fn (E) -> Result[T, R]) -> Result[T, R] {
match self {
case Ok(v) -> Result.Ok(v)
case Error(e) -> block.call(e)
}
}
fn pub move else[R](block: fn (E) -> Result[T, R]) -> Result[T, R]
Maps a Result[T, E]
into a Result[T, R]
.
If self
is an Error
, the supplied closure is called and its returned
Result
is returned. If self
is an Ok
, the Ok
is returned as-is.
Examples
let foo: Result[Int, String] = Result.Error('oops!')
res.else fn (val) {
Result.Error(val.to_upper)
}
# => Result.Error('OOPS!')
error
Show source codeHide source code
fn pub move error -> Option[E] {
match self {
case Error(v) -> Option.Some(v)
case _ -> Option.None
}
}
fn pub move error -> Option[E]
Converts self
into an Option[E]
.
If self
is an Error
, a Some(E)
is returned, otherwise a None
is
returned.
Examples
let res: Result[Int, String] = Result.Error('oops!')
res.error # => Option.Some(42)
error?
Show source codeHide source code
fn pub error? -> Bool {
match self {
case Error(_) -> true
case _ -> false
}
}
fn pub error? -> Bool
Returns true
if self
is an Err
.
Examples
let res: Result[Int, String] = Result.Error('oops!')
res.error? # => true
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
match self {
case Ok(v) -> formatter.tuple('Ok').field(v).finish
case Error(v) -> formatter.tuple('Error').field(v).finish
}
}
fn pub fmt(formatter: mut Formatter)
if
E: Format,
T: Format
Formats self
in a human-readable format for debugging purposes.
get
Show source codeHide source code
fn pub move get -> T {
or_panic('Result.get expects an Ok(_), but an Error(_) is found')
}
fn pub move get -> T
Returns the value wrapped by the Ok
case, or panics if self
is an
Error
.
Panics
This method panics if self
is an Error
.
Examples
let res: Result[Int, String] = Result.Ok(42)
res.get # => 42
map
Show source codeHide source code
fn pub move map[R](block: fn (T) -> R) -> Result[R, E] {
match self {
case Ok(v) -> Result.Ok(block.call(v))
case Error(e) -> Result.Error(e)
}
}
fn pub move map[R](block: fn (T) -> R) -> Result[R, E]
Maps a Result[T, E]
into a Result[R, E]
.
If self
is an Ok
, the supplied closure is called and its value used to
return a new Ok
. If self
is an Error
, the Error
is returned as-is.
Examples
let foo: Result[Int, String] = Result.Ok(42)
res.map fn (val) { val.to_string } # => Result.Ok('42')
map_error
Show source codeHide source code
fn pub move map_error[R](block: fn (E) -> R) -> Result[T, R] {
match self {
case Ok(v) -> Result.Ok(v)
case Error(e) -> Result.Error(block.call(e))
}
}
fn pub move map_error[R](block: fn (E) -> R) -> Result[T, R]
Maps a Result[T, E]
into a Result[T, R]
.
If self
is an Error
, the supplied closure is called and its value used
to return a new Error
. If self
is an Ok
, the Ok
is returned as-is.
Examples
let foo: Result[Int, String] = Result.Error('oops!')
res.map_error fn (val) { val.to_upper } # => Result.Ok('OOPS!')
ok
Show source codeHide source code
fn pub move ok -> Option[T] {
match self {
case Ok(v) -> Option.Some(v)
case _ -> Option.None
}
}
fn pub move ok -> Option[T]
Converts self
into an Option[T]
.
If self
is an Ok
, a Some(T)
is returned, otherwise a None
is
returned.
Examples
let res: Result[Int, String] = Result.Ok(42)
res.ok # => Option.Some(42)
ok?
Show source codeHide source code
fn pub ok? -> Bool {
match self {
case Ok(_) -> true
case _ -> false
}
}
fn pub ok? -> Bool
Returns true
if self
is an Ok
.
Examples
let res: Result[Int, String] = Result.Ok(42)
res.ok? # => true
or
Show source codeHide source code
fn pub move or(default: T) -> T {
match self {
case Ok(val) -> val
case _ -> default
}
}
fn pub move or(default: T) -> T
Returns the value wrapped by Ok
, or returns default
if self
is an
Error
.
Examples
let foo: Result[Int, String] = Result.Ok(42)
let bar: Result[Int, String] = Result.Error('oops!')
foo.or(0) # => 42
bar.or(0) # => 0
or_else
Show source codeHide source code
fn pub move or_else(block: fn (E) -> T) -> T {
match self {
case Ok(v) -> v
case Error(v) -> block.call(v)
}
}
fn pub move or_else(block: fn (E) -> T) -> T
Returns the value wrapped by Ok
, or returns the closure's return value if
self
is an Error
.
The argument passed to the closure is the value wrapped by the Error
case.
Examples
let foo: Result[Int, String] = Result.Ok(42)
let bar: Result[Int, String] = Result.Error('oops!')
foo.or_else fn (_) { 0 } # => 42
bar.or_else fn (_) { 0 } # => 0
or_panic
Show source codeHide source code
fn pub move or_panic(message: String) -> T {
match self {
case Ok(v) -> v
case _ -> panic(message)
}
}
fn pub move or_panic(message: String) -> T
Returns the value wrapped by Ok
, or panics with the given message if
self
is an Error
.
Examples
let res: Result[Int, String] = Result.Ok(10)
res.or_panic('a number must be present') # => 10
then
Show source codeHide source code
fn pub move then[R](block: fn (T) -> Result[R, E]) -> Result[R, E] {
match self {
case Ok(v) -> block.call(v)
case Error(e) -> Result.Error(e)
}
}
fn pub move then[R](block: fn (T) -> Result[R, E]) -> Result[R, E]
Maps a Result[T, E]
into a Result[R, E]
.
If self
is an Ok
, the supplied closure is called and its returned
Result
is returned. If self
is an Error
, the Error
is returned
as-is.
Examples
let foo: Result[Int, String] = Result.Ok(42)
res.then fn (val) { Result.Ok(val.to_string) } # => Result.Ok('42')
Implemented traits
Clone
impl Clone[Result[T, E]] for Result
if
E: Clone[E],
T: Clone[T]
Equal
impl Equal[ref Result[T, E]] for Result
if
E: Equal[ref E],
T: Equal[ref T]
Format
impl Format for Result
if
E: Format,
T: Format