@@ -792,8 +792,7 @@ impl<T, E> Result<T, E> {
792
792
}
793
793
}
794
794
795
- /// Unwraps a result, yielding the content of an [`Ok`].
796
- /// Else, it returns `optb`.
795
+ /// Returns the contained [`Ok`] value or a provided default.
797
796
///
798
797
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
799
798
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
@@ -808,27 +807,25 @@ impl<T, E> Result<T, E> {
808
807
/// Basic usage:
809
808
///
810
809
/// ```
811
- /// let optb = 2;
810
+ /// let default = 2;
812
811
/// let x: Result<u32, &str> = Ok(9);
813
- /// assert_eq!(x.unwrap_or(optb ), 9);
812
+ /// assert_eq!(x.unwrap_or(default ), 9);
814
813
///
815
814
/// let x: Result<u32, &str> = Err("error");
816
- /// assert_eq!(x.unwrap_or(optb ), optb );
815
+ /// assert_eq!(x.unwrap_or(default ), default );
817
816
/// ```
818
817
#[ inline]
819
818
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
820
- pub fn unwrap_or ( self , optb : T ) -> T {
819
+ pub fn unwrap_or ( self , default : T ) -> T {
821
820
match self {
822
821
Ok ( t) => t,
823
- Err ( _) => optb ,
822
+ Err ( _) => default ,
824
823
}
825
824
}
826
825
827
- /// Unwraps a result, yielding the content of an [`Ok`].
828
- /// If the value is an [`Err`] then it calls `op` with its value.
826
+ /// Returns the contained [`Ok`] value or computes it from a closure.
829
827
///
830
828
/// [`Ok`]: enum.Result.html#variant.Ok
831
- /// [`Err`]: enum.Result.html#variant.Err
832
829
///
833
830
/// # Examples
834
831
///
@@ -931,7 +928,7 @@ impl<T: Clone, E> Result<&mut T, E> {
931
928
}
932
929
933
930
impl < T , E : fmt:: Debug > Result < T , E > {
934
- /// Unwraps a result, yielding the content of an [`Ok`].
931
+ /// Returns the contained [`Ok`] value, consuming the `self` value .
935
932
///
936
933
/// # Panics
937
934
///
@@ -959,7 +956,16 @@ impl<T, E: fmt::Debug> Result<T, E> {
959
956
}
960
957
}
961
958
962
- /// Unwraps a result, yielding the content of an [`Ok`].
959
+ /// Returns the contained [`Ok`] value, consuming the `self` value.
960
+ ///
961
+ /// Because this function may panic, its use is generally discouraged.
962
+ /// Instead, prefer to use pattern matching and handle the [`Err`]
963
+ /// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
964
+ /// [`unwrap_or_default`].
965
+ ///
966
+ /// [`unwrap_or`]: #method.unwrap_or
967
+ /// [`unwrap_or_else`]: #method.unwrap_or_else
968
+ /// [`unwrap_or_default`]: #method.unwrap_or_default
963
969
///
964
970
/// # Panics
965
971
///
@@ -994,7 +1000,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
994
1000
}
995
1001
996
1002
impl < T : fmt:: Debug , E > Result < T , E > {
997
- /// Unwraps a result, yielding the content of an [`Err`].
1003
+ /// Returns the contained [`Err`] value, consuming the `self` value .
998
1004
///
999
1005
/// # Panics
1000
1006
///
@@ -1022,7 +1028,7 @@ impl<T: fmt::Debug, E> Result<T, E> {
1022
1028
}
1023
1029
}
1024
1030
1025
- /// Unwraps a result, yielding the content of an [`Err`].
1031
+ /// Returns the contained [`Err`] value, consuming the `self` value .
1026
1032
///
1027
1033
/// # Panics
1028
1034
///
@@ -1056,7 +1062,7 @@ impl<T: fmt::Debug, E> Result<T, E> {
1056
1062
}
1057
1063
1058
1064
impl < T : Default , E > Result < T , E > {
1059
- /// Returns the contained value or a default
1065
+ /// Returns the contained [`Ok`] value or a default
1060
1066
///
1061
1067
/// Consumes the `self` argument then, if [`Ok`], returns the contained
1062
1068
/// value, otherwise if [`Err`], returns the default value for that
@@ -1095,7 +1101,7 @@ impl<T: Default, E> Result<T, E> {
1095
1101
1096
1102
#[ unstable( feature = "unwrap_infallible" , reason = "newly added" , issue = "61695" ) ]
1097
1103
impl < T , E : Into < !> > Result < T , E > {
1098
- /// Unwraps a result that can never be an [`Err`], yielding the content of the [`Ok`] .
1104
+ /// Returns the contained [`Ok`] value, but never panics .
1099
1105
///
1100
1106
/// Unlike [`unwrap`], this method is known to never panic on the
1101
1107
/// result types it is implemented for. Therefore, it can be used
0 commit comments