pub struct OnceOption<T> { /* private fields */ }Expand description
OnceOption represents an optional value. Differently from Option, an empty OnceOption cannot be re-set to contain a value.
Check the crate level documentation for more details.
Implementations§
Source§impl<T> OnceOption<T>
impl<T> OnceOption<T>
Sourcepub const NONE: Self
pub const NONE: Self
A constant representing a OnceOption that doesn’t contain any value.
Since a OnceOption cannot be set to contain a value after it has been
emptied, this constant is provided as a helper, but is of dubious utility.
§Examples
let x: OnceOption<u32> = OnceOption::NONE;
assert_eq!(x.is_some(), false);Sourcepub const fn is_some(&self) -> bool
pub const fn is_some(&self) -> bool
Returns true if the once-option contains a value.
§Examples
let x: OnceOption<u32> = OnceOption(1912);
assert_eq!(x.is_some(), true);
let x: OnceOption<u32> = OnceOption::NONE;
assert_eq!(x.is_some(), false);Sourcepub const fn is_none(&self) -> bool
pub const fn is_none(&self) -> bool
Returns true if the option is empty.
§Examples
let x: Option<u32> = Some(2);
assert_eq!(x.is_none(), false);
let x: Option<u32> = None;
assert_eq!(x.is_none(), true);Sourcepub fn expect(self, msg: &str) -> T
pub fn expect(self, msg: &str) -> T
Returns the contained value, consuming the self value.
§Panics
Panics if the value is empty with a custom panic message provided by
msg.
§Examples
let x = OnceOption("value");
assert_eq!(x.expect("fruits are healthy"), "value");let x = OnceOption::<&str>::NONE;
x.expect("fruits are healthy"); // panics with `fruits are healthy`Sourcepub fn expect_none(self, msg: &str)
pub fn expect_none(self, msg: &str)
Panics if the OnceOption is not empty and does contain a value.
§Panics
Panics if the value is not empty, with a custom panic message provided by
msg.
§Examples
let x = OnceOption("value");
assert_eq!(x.expect("fruits are healthy"), "value");let x = OnceOption::<&str>("something something");
x.expect_none("fruits are healthy"); // panics with `fruits are healthy`Sourcepub fn take(&mut self) -> T
pub fn take(&mut self) -> T
Takes the value out of the OnceOption, leaving it empty. If the
OnceOption is already empty, this function will panic.
Note that this operation cannot be reversed: once a OnceOption becomes
empty, it cannot get a value again.
§Examples
let mut x = OnceOption(2);
let y = x.take();
assert!(x.is_none());
assert_eq!(y, 2);let mut x = OnceOption(2);
let y = x.take();
let w = x.take(); // this panics!Sourcepub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Returns the contained value, consuming the self value. If the
OnceOption is empty, this function will panic.
§Examples
let mut x = OnceOption(2);
assert_eq!(*x, 2);
x.replace(5);
assert_eq!(*x, 5);let mut x: OnceOption<u32> = OnceOption::NONE;
x.unwrap(); // this panics!Sourcepub fn replace(&mut self, value: T) -> T
pub fn replace(&mut self, value: T) -> T
Replaces the actual value in the option by the value given in parameter, returning the old value if present, or panic’ing otherwise.
§Examples
let mut x = OnceOption(2);
let old = x.replace(5);
assert_eq!(x.take(), 5);
assert_eq!(old, 2);let mut x: OnceOption<i32> = OnceOption::NONE;
let _ = x.replace(3); // this panicsTrait Implementations§
Source§impl<T: Binary> Binary for OnceOption<T>
impl<T: Binary> Binary for OnceOption<T>
Source§impl<T: Clone> Clone for OnceOption<T>
impl<T: Clone> Clone for OnceOption<T>
Source§fn clone(&self) -> OnceOption<T>
fn clone(&self) -> OnceOption<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for OnceOption<T>
impl<T: Debug> Debug for OnceOption<T>
Source§impl<T> Default for OnceOption<T>
impl<T> Default for OnceOption<T>
Source§fn default() -> OnceOption<T>
fn default() -> OnceOption<T>
Returns an empty OnceOption.
§Examples
let opt: OnceOption<u32> = OnceOption::default();
assert!(opt.is_none());Source§impl<T> Deref for OnceOption<T>
impl<T> Deref for OnceOption<T>
Source§impl<T> DerefMut for OnceOption<T>
impl<T> DerefMut for OnceOption<T>
Source§impl<T: Display> Display for OnceOption<T>
impl<T: Display> Display for OnceOption<T>
Source§impl<T> From<Option<T>> for OnceOption<T>
impl<T> From<Option<T>> for OnceOption<T>
Source§fn from(val: Option<T>) -> OnceOption<T>
fn from(val: Option<T>) -> OnceOption<T>
Moves an option val into a new OnceOption.
§Examples
let o: OnceOption<u8> = OnceOption::from(Some(67));
assert_eq!(67, *o);Source§impl<T> From<T> for OnceOption<T>
impl<T> From<T> for OnceOption<T>
Source§fn from(val: T) -> OnceOption<T>
fn from(val: T) -> OnceOption<T>
Moves val into a new OnceOption.
§Examples
let o: OnceOption<u8> = OnceOption::from(67);
assert_eq!(67, *o);Alternatively:
let o: OnceOption<u8> = 67.into();
assert_eq!(67, *o);