@@ -1077,6 +1077,43 @@ impl<T, E> Result<T, E> {
1077
1077
}
1078
1078
}
1079
1079
1080
+ /// Returns the contained [`Ok`] value or a default
1081
+ ///
1082
+ /// Consumes the `self` argument then, if [`Ok`], returns the contained
1083
+ /// value, otherwise if [`Err`], returns the default value for that
1084
+ /// type.
1085
+ ///
1086
+ /// # Examples
1087
+ ///
1088
+ /// Converts a string to an integer, turning poorly-formed strings
1089
+ /// into 0 (the default value for integers). [`parse`] converts
1090
+ /// a string to any other type that implements [`FromStr`], returning an
1091
+ /// [`Err`] on error.
1092
+ ///
1093
+ /// ```
1094
+ /// let good_year_from_input = "1909";
1095
+ /// let bad_year_from_input = "190blarg";
1096
+ /// let good_year = good_year_from_input.parse().unwrap_or_default();
1097
+ /// let bad_year = bad_year_from_input.parse().unwrap_or_default();
1098
+ ///
1099
+ /// assert_eq!(1909, good_year);
1100
+ /// assert_eq!(0, bad_year);
1101
+ /// ```
1102
+ ///
1103
+ /// [`parse`]: str::parse
1104
+ /// [`FromStr`]: crate::str::FromStr
1105
+ #[ inline]
1106
+ #[ stable( feature = "result_unwrap_or_default" , since = "1.16.0" ) ]
1107
+ pub fn unwrap_or_default ( self ) -> T
1108
+ where
1109
+ T : Default ,
1110
+ {
1111
+ match self {
1112
+ Ok ( x) => x,
1113
+ Err ( _) => Default :: default ( ) ,
1114
+ }
1115
+ }
1116
+
1080
1117
////////////////////////////////////////////////////////////////////////
1081
1118
// Boolean operations on the values, eager and lazy
1082
1119
/////////////////////////////////////////////////////////////////////////
@@ -1458,42 +1495,6 @@ impl<T: fmt::Debug, E> Result<T, E> {
1458
1495
}
1459
1496
}
1460
1497
1461
- impl < T : Default , E > Result < T , E > {
1462
- /// Returns the contained [`Ok`] value or a default
1463
- ///
1464
- /// Consumes the `self` argument then, if [`Ok`], returns the contained
1465
- /// value, otherwise if [`Err`], returns the default value for that
1466
- /// type.
1467
- ///
1468
- /// # Examples
1469
- ///
1470
- /// Converts a string to an integer, turning poorly-formed strings
1471
- /// into 0 (the default value for integers). [`parse`] converts
1472
- /// a string to any other type that implements [`FromStr`], returning an
1473
- /// [`Err`] on error.
1474
- ///
1475
- /// ```
1476
- /// let good_year_from_input = "1909";
1477
- /// let bad_year_from_input = "190blarg";
1478
- /// let good_year = good_year_from_input.parse().unwrap_or_default();
1479
- /// let bad_year = bad_year_from_input.parse().unwrap_or_default();
1480
- ///
1481
- /// assert_eq!(1909, good_year);
1482
- /// assert_eq!(0, bad_year);
1483
- /// ```
1484
- ///
1485
- /// [`parse`]: str::parse
1486
- /// [`FromStr`]: crate::str::FromStr
1487
- #[ inline]
1488
- #[ stable( feature = "result_unwrap_or_default" , since = "1.16.0" ) ]
1489
- pub fn unwrap_or_default ( self ) -> T {
1490
- match self {
1491
- Ok ( x) => x,
1492
- Err ( _) => Default :: default ( ) ,
1493
- }
1494
- }
1495
- }
1496
-
1497
1498
#[ unstable( feature = "unwrap_infallible" , reason = "newly added" , issue = "61695" ) ]
1498
1499
impl < T , E : Into < !> > Result < T , E > {
1499
1500
/// Returns the contained [`Ok`] value, but never panics.
0 commit comments