Add Option::get_or_default
This commit is contained in:
parent
1d6b0f626a
commit
7e3ebe76ee
1 changed files with 28 additions and 0 deletions
|
@ -854,6 +854,34 @@ impl<T> Option<T> {
|
||||||
// Entry-like operations to insert if None and return a reference
|
// Entry-like operations to insert if None and return a reference
|
||||||
/////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// Inserts the default value into the option if it is [`None`], then
|
||||||
|
/// returns a mutable reference to the contained value.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #![feature(option_get_or_default)]
|
||||||
|
///
|
||||||
|
/// let mut x = None;
|
||||||
|
///
|
||||||
|
/// {
|
||||||
|
/// let y: &mut u32 = x.get_or_default();
|
||||||
|
/// assert_eq!(y, &0);
|
||||||
|
///
|
||||||
|
/// *y = 7;
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// assert_eq!(x, Some(7));
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
#[unstable(feature = "option_get_or_default", issue = "82901")]
|
||||||
|
pub fn get_or_default(&mut self) -> &mut T
|
||||||
|
where
|
||||||
|
T: Default,
|
||||||
|
{
|
||||||
|
self.get_or_insert_with(Default::default)
|
||||||
|
}
|
||||||
|
|
||||||
/// Inserts `value` into the option if it is [`None`], then
|
/// Inserts `value` into the option if it is [`None`], then
|
||||||
/// returns a mutable reference to the contained value.
|
/// returns a mutable reference to the contained value.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue