OnceOption

Struct OnceOption 

Source
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>

Source

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);
Source

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);
Source

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);
Source

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`
Source

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`
Source

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!
Source

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!
Source

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 panics

Trait Implementations§

Source§

impl<T: Binary> Binary for OnceOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone> Clone for OnceOption<T>

Source§

fn clone(&self) -> OnceOption<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for OnceOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for OnceOption<T>

Source§

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>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for OnceOption<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T: Display> Display for OnceOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<Option<T>> for OnceOption<T>

Source§

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>

Source§

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);
Source§

impl<T: Hash> Hash for OnceOption<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: LowerExp> LowerExp for OnceOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: LowerHex> LowerHex for OnceOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Octal> Octal for OnceOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Ord> Ord for OnceOption<T>

Source§

fn cmp(&self, other: &OnceOption<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for OnceOption<T>

Source§

fn eq(&self, other: &OnceOption<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for OnceOption<T>

Source§

fn partial_cmp(&self, other: &OnceOption<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: UpperExp> UpperExp for OnceOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: UpperHex> UpperHex for OnceOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Copy> Copy for OnceOption<T>

Source§

impl<T: Eq> Eq for OnceOption<T>

Source§

impl<T> StructuralPartialEq for OnceOption<T>

Auto Trait Implementations§

§

impl<T> Freeze for OnceOption<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for OnceOption<T>
where T: RefUnwindSafe,

§

impl<T> Send for OnceOption<T>
where T: Send,

§

impl<T> Sync for OnceOption<T>
where T: Sync,

§

impl<T> Unpin for OnceOption<T>
where T: Unpin,

§

impl<T> UnwindSafe for OnceOption<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.