// Entry-like operations to insert if None and return a reference
/////////////////////////////////////////////////////////////////////////
- /// Inserts the default 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.
///
/// # Examples
///
/// ```
- /// #![feature(option_get_or_default)]
- ///
/// let mut x = None;
///
/// {
- /// let y: &mut u32 = x.get_or_default();
- /// assert_eq!(y, &0);
+ /// let y: &mut u32 = x.get_or_insert(5);
+ /// assert_eq!(y, &5);
///
/// *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)
+ #[stable(feature = "option_entry", since = "1.20.0")]
+ pub fn get_or_insert(&mut self, value: T) -> &mut T {
+ self.get_or_insert_with(|| value)
}
- /// Inserts `value` into the option if it is [`None`], then
+ /// 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_insert_default)]
+ ///
/// let mut x = None;
///
/// {
- /// let y: &mut u32 = x.get_or_insert(5);
- /// assert_eq!(y, &5);
+ /// let y: &mut u32 = x.get_or_insert_default();
+ /// assert_eq!(y, &0);
///
/// *y = 7;
/// }
/// assert_eq!(x, Some(7));
/// ```
#[inline]
- #[stable(feature = "option_entry", since = "1.20.0")]
- pub fn get_or_insert(&mut self, value: T) -> &mut T {
- self.get_or_insert_with(|| value)
+ #[unstable(feature = "option_get_or_insert_default", issue = "82901")]
+ pub fn get_or_insert_default(&mut self) -> &mut T
+ where
+ T: Default,
+ {
+ self.get_or_insert_with(Default::default)
}
/// Inserts a value computed from `f` into the option if it is [`None`],