@@ -810,6 +810,45 @@ impl<T> Option<T> {
810
810
}
811
811
}
812
812
813
+ /// Returns the contained [`Some`] value or a default.
814
+ ///
815
+ /// Consumes the `self` argument then, if [`Some`], returns the contained
816
+ /// value, otherwise if [`None`], returns the [default value] for that
817
+ /// type.
818
+ ///
819
+ /// # Examples
820
+ ///
821
+ /// Converts a string to an integer, turning poorly-formed strings
822
+ /// into 0 (the default value for integers). [`parse`] converts
823
+ /// a string to any other type that implements [`FromStr`], returning
824
+ /// [`None`] on error.
825
+ ///
826
+ /// ```
827
+ /// let good_year_from_input = "1909";
828
+ /// let bad_year_from_input = "190blarg";
829
+ /// let good_year = good_year_from_input.parse().ok().unwrap_or_default();
830
+ /// let bad_year = bad_year_from_input.parse().ok().unwrap_or_default();
831
+ ///
832
+ /// assert_eq!(1909, good_year);
833
+ /// assert_eq!(0, bad_year);
834
+ /// ```
835
+ ///
836
+ /// [default value]: Default::default
837
+ /// [`parse`]: str::parse
838
+ /// [`FromStr`]: crate::str::FromStr
839
+ #[ inline]
840
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
841
+ #[ rustc_const_unstable( feature = "const_option_ext" , issue = "91930" ) ]
842
+ pub const fn unwrap_or_default ( self ) -> T
843
+ where
844
+ T : ~const Default ,
845
+ {
846
+ match self {
847
+ Some ( x) => x,
848
+ None => Default :: default ( ) ,
849
+ }
850
+ }
851
+
813
852
/// Returns the contained [`Some`] value, consuming the `self` value,
814
853
/// without checking that the value is not [`None`].
815
854
///
@@ -1685,47 +1724,6 @@ impl<T: Clone> Option<&mut T> {
1685
1724
}
1686
1725
}
1687
1726
1688
- impl < T : Default > Option < T > {
1689
- /// Returns the contained [`Some`] value or a default.
1690
- ///
1691
- /// Consumes the `self` argument then, if [`Some`], returns the contained
1692
- /// value, otherwise if [`None`], returns the [default value] for that
1693
- /// type.
1694
- ///
1695
- /// # Examples
1696
- ///
1697
- /// Converts a string to an integer, turning poorly-formed strings
1698
- /// into 0 (the default value for integers). [`parse`] converts
1699
- /// a string to any other type that implements [`FromStr`], returning
1700
- /// [`None`] on error.
1701
- ///
1702
- /// ```
1703
- /// let good_year_from_input = "1909";
1704
- /// let bad_year_from_input = "190blarg";
1705
- /// let good_year = good_year_from_input.parse().ok().unwrap_or_default();
1706
- /// let bad_year = bad_year_from_input.parse().ok().unwrap_or_default();
1707
- ///
1708
- /// assert_eq!(1909, good_year);
1709
- /// assert_eq!(0, bad_year);
1710
- /// ```
1711
- ///
1712
- /// [default value]: Default::default
1713
- /// [`parse`]: str::parse
1714
- /// [`FromStr`]: crate::str::FromStr
1715
- #[ inline]
1716
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1717
- #[ rustc_const_unstable( feature = "const_option_ext" , issue = "91930" ) ]
1718
- pub const fn unwrap_or_default ( self ) -> T
1719
- where
1720
- T : ~const Default ,
1721
- {
1722
- match self {
1723
- Some ( x) => x,
1724
- None => Default :: default ( ) ,
1725
- }
1726
- }
1727
- }
1728
-
1729
1727
impl < T : Deref > Option < T > {
1730
1728
/// Converts from `Option<T>` (or `&Option<T>`) to `Option<&T::Target>`.
1731
1729
///
0 commit comments