pub trait OptionExt<T> {
// Required methods
fn err_or<U>(self, ok: U) -> Result<U, T>;
fn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T>;
fn err_or_default<U: Default>(self) -> Result<U, T>;
fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool;
fn expect_none(self, msg: &str);
fn tap_mut(self, f: impl FnOnce(&mut T)) -> Option<T>;
}
Expand description
Extension methods for std::option::Option
.
Required Methods§
Sourcefn err_or<U>(self, ok: U) -> Result<U, T>
fn err_or<U>(self, ok: U) -> Result<U, T>
Converts an Option
to a Result
.
Unlike the standard library,
the value of Option
is treated as Result::Err
instead of Result::Ok
.
Arguments passed to err_or
are eagerly evaluated;
if you are passing the result of a function call,
it is recommended to use OptionExt::err_or_else
, which is lazily evaluated.
§Example
use rs_std_ext::option::OptionExt;
assert_eq!(Some("err").err_or(0), Err("err"));
assert_eq!(None::<&str>.err_or(0), Ok(0));
Sourcefn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T>
fn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T>
Converts an Option
to a Result
.
Unlike the standard library,
the value of Option
is treated as Result::Err
instead of Result::Ok
.
§Example
use rs_std_ext::option::OptionExt;
assert_eq!(Some("err").err_or_else(|| 0), Err("err"));
assert_eq!(None::<&str>.err_or_else(|| 0), Ok(0));
Sourcefn err_or_default<U: Default>(self) -> Result<U, T>
fn err_or_default<U: Default>(self) -> Result<U, T>
Converts an Option
to a Result
.
Unlike the standard library,
the value of Option
is treated as Result::Err
instead of Result::Ok
.
§Example
use rs_std_ext::option::OptionExt;
assert_eq!(Some("err").err_or_default::<usize>(), Err("err"));
assert_eq!(None::<&str>.err_or_default::<usize>(), Ok(0));
Sourcefn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool
👎Deprecated: Use Option::is_none_or
(offered by std) instead.
fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool
Option::is_none_or
(offered by std) instead.Returns true
if the Option
is a None
or the value inside of it matches a predicate.
This is equivalent to option.is_some_and(!f)
.
§Example
use rs_std_ext::option::OptionExt;
assert_eq!(None::<usize>.is_none_or(|&i| i == 2), true);
assert_eq!(Some(5).is_none_or(|&i| i == 2), false);
Sourcefn expect_none(self, msg: &str)
fn expect_none(self, msg: &str)
Returns nothing, consuming the value contained.
§Panics
Panics if the value is a Some
with a custom panic message provided by msg
.
§Example
use rs_std_ext::option::OptionExt;
let x: Option<&str> = Some("10");
x.expect_none("fruits are healthy");// panics with `fruits are healthy`
§Recommended Message Style
See the documentation for Option::expect
for more information.
Sourcefn tap_mut(self, f: impl FnOnce(&mut T)) -> Option<T>
fn tap_mut(self, f: impl FnOnce(&mut T)) -> Option<T>
Tap into an Option
and modify its value.
This method captures the Option
in order not to break the caller-chain.
§Example
use rs_std_ext::option::OptionExt;
let mut x: Option<String> = Some("4".into());
assert_eq!(x.tap_mut(|s| s.push('2')), Some("42".into()));
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<T> OptionExt<T> for Option<T>
impl<T> OptionExt<T> for Option<T>
fn err_or<U>(self, ok: U) -> Result<U, T>
fn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T>
fn err_or_default<U: Default>(self) -> Result<U, T>
Source§fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool
fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool
Option::is_none_or
(offered by std) instead.