Returns None if the option is None
, otherwise calls f with the wrapped
value and returns the result.
Some languages call this operation flatmap.
const some = Some(25); const sq = (x: number) => Some(x * x);
// 25 * 25 => 625 + 5 => 630
const result = some.andThen(sq).andThen((x) => Some(x + 5));
expect(result.unwrap()).toEqual(630);
Returns the contained Some value, consuming the self value.
Panics if the value is a None
with a custom panic message provided by msg. [Error
]
Returns
None
if the option isNone
, otherwise callspredicate
with the wrapped value and returns: - [Some(t)
] ifpredicate
returnstrue
(wheret
is the wrapped value), and -None
ifpredicate
returnsfalse
.
const result = Some({ status: 200 })
.filter((item) => item.status === 200)
.map((_) => "Ok")
.unwrapOr("Error");
expect(result).toEqual("Ok");
expect(Some(200).filter((item) => item === 200).unwrapOr(500)).toEqual(200);
Converts from Option<Option
expect(Some(Some(Some(50))).flatten()).toEqual(Some(Some(50)));
expect(Some(Some(50)).flatten()).toEqual(Some(50));
expect(Some(50).flatten()).toEqual(Some(50));
expect(Some(50).flatten().unwrap()).toEqual(50);
expect(Some(None()).flatten()).toEqual(None());
expect(None().flatten()).toEqual(None());
Returns true
if the option is a None
value.
Returns true
if the option is a Some
value.
Applies a function to the contained value (if any), or returns the provided default (if not).
const defaultStatus: number = 500;
const some = Some({ status: 200 });
const mappedSome = some.mapOr(defaultStatus, (data) => data.status);
expect(mappedSome).toEqual(200);
Applies a function to the contained value (if any), or computes a default (if not).
const defaultStatus: number = 500;
const some = Some({ status: 200 });
const mappedSome = some.mapOrElse(() => defaultStatus, (data) => data.status);
expect(mappedSome).toEqual(200);
const none = None();
const mappedNone = none.mapOrElse(() => defaultStatus, (data) => data.status);
expect(mappedNone).toEqual(500);
Pattern match to retrieve the value
return type of the Some
branch
return type of the None
branch
expect(Some("ok").match({
some: some => some.length,
none: () => "error",
})).toEqual(2);
expect(None().match({
some: _ => "some",
none: () => "Something bad wrong",
})).toEqual("Something bad wrong")
expect(None().match({
some: _ => 200,
none: () => 404,
})).toEqual(404)
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err)
.
Arguments passed to okOr
are eagerly evaluated; if you are passing the result of a function
call, it is recommended to use okOrElse
, which is lazily evaluated.
expect(Some(5).okOr("Failed")).toEqual(Ok(5));
expect(None().okOr("Failed")).toEqual(Err("Failed"));
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err())
.
const failFn = () => "Failed";
expect(Some(5).okOrElse(failFn)).toEqual(Ok(5));
expect(None().okOrElse(failFn)).toEqual(Err("Failed"));
Replaces the actual value in the option by the value given in parameter,
returning the old value if present, leaving a Some
in its place without
deinitializing either one.
expect(Some(50).unwrap()).toEqual(50);
const oldSome = some.replace(250); expect(oldSome.unwrap()).toEqual(50);
expect(some.unwrap()).toEqual(250);
Returns a string representation of an object.
Transposes an Option
of a Result
into a Result
of an Option
.
None
will be mapped to Ok(None)
. Some(Ok(_))
and Some(Err(_))
will
be mapped to Ok(Some(_))
and Err(_)
.
Some
where self is not an Result
,with a panic message provided by the Some
's value.
const x: Result<Option<number>, string> = Ok(Some(5));
const y: Option<Result<number, string>> = Some(Ok(5));
expect(x).toEqual(y.transpose());
Inserts value into the option
If the option already contains a value, the old value is dropped.
Returns the contained Some
value or a provided default.
const some = Some("SOME");
expect(some.unwrapOrElse(() => "NONE")).toEqual("SOME");
Returns the contained Some
value or computes it from a closure.
expect(None().unwrapOrElse(() => "NONE")).toEqual("NONE");
Zips self
with another Option
.
If self
is Some(s)
and other is Some(o)
, this method returns Some((s, o))
. Otherwise, None
is returned.
const x = Some(1);
const y = Some("hi");
const z = None();
expect(x.zip(y)).toEqual(Some([1, "hi"])); expect(x.zip(z)).toEqual(None());
Returns the "default value" for a OptionNone
.
Generated using TypeDoc
Type
Option
represents an optional value: everyOption
is eitherSome
and contains a value, orNone
, and does not.Option
types are very common in Rust code, as they have a number of uses:None
is returned on error