@@ -798,8 +798,7 @@ impl<T, E> Result<T, E> {
798
798
}
799
799
}
800
800
801
- /// Unwraps a result, yielding the content of an [`Ok`].
802
- /// Else, it returns `optb`.
801
+ /// Returns the contained [`Ok`] value or a provided default.
803
802
///
804
803
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
805
804
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
@@ -814,27 +813,25 @@ impl<T, E> Result<T, E> {
814
813
/// Basic usage:
815
814
///
816
815
/// ```
817
- /// let optb = 2;
816
+ /// let default = 2;
818
817
/// let x: Result<u32, &str> = Ok(9);
819
- /// assert_eq!(x.unwrap_or(optb ), 9);
818
+ /// assert_eq!(x.unwrap_or(default ), 9);
820
819
///
821
820
/// let x: Result<u32, &str> = Err("error");
822
- /// assert_eq!(x.unwrap_or(optb ), optb );
821
+ /// assert_eq!(x.unwrap_or(default ), default );
823
822
/// ```
824
823
#[ inline]
825
824
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
826
- pub fn unwrap_or ( self , optb : T ) -> T {
825
+ pub fn unwrap_or ( self , default : T ) -> T {
827
826
match self {
828
827
Ok ( t) => t,
829
- Err ( _) => optb ,
828
+ Err ( _) => default ,
830
829
}
831
830
}
832
831
833
- /// Unwraps a result, yielding the content of an [`Ok`].
834
- /// If the value is an [`Err`] then it calls `op` with its value.
832
+ /// Returns the contained [`Ok`] value or computes it from a closure.
835
833
///
836
834
/// [`Ok`]: enum.Result.html#variant.Ok
837
- /// [`Err`]: enum.Result.html#variant.Err
838
835
///
839
836
/// # Examples
840
837
///
@@ -937,7 +934,44 @@ impl<T: Clone, E> Result<&mut T, E> {
937
934
}
938
935
939
936
impl < T , E : fmt:: Debug > Result < T , E > {
940
- /// Unwraps a result, yielding the content of an [`Ok`].
937
+ /// Returns the contained [`Ok`] value, consuming the `self` value.
938
+ ///
939
+ /// # Panics
940
+ ///
941
+ /// Panics if the value is an [`Err`], with a panic message including the
942
+ /// passed message, and the content of the [`Err`].
943
+ ///
944
+ /// [`Ok`]: enum.Result.html#variant.Ok
945
+ /// [`Err`]: enum.Result.html#variant.Err
946
+ ///
947
+ /// # Examples
948
+ ///
949
+ /// Basic usage:
950
+ ///
951
+ /// ```{.should_panic}
952
+ /// let x: Result<u32, &str> = Err("emergency failure");
953
+ /// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
954
+ /// ```
955
+ #[ inline]
956
+ #[ track_caller]
957
+ #[ stable( feature = "result_expect" , since = "1.4.0" ) ]
958
+ pub fn expect ( self , msg : & str ) -> T {
959
+ match self {
960
+ Ok ( t) => t,
961
+ Err ( e) => unwrap_failed ( msg, & e) ,
962
+ }
963
+ }
964
+
965
+ /// Returns the contained [`Ok`] value, consuming the `self` value.
966
+ ///
967
+ /// Because this function may panic, its use is generally discouraged.
968
+ /// Instead, prefer to use pattern matching and handle the [`Err`]
969
+ /// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
970
+ /// [`unwrap_or_default`].
971
+ ///
972
+ /// [`unwrap_or`]: #method.unwrap_or
973
+ /// [`unwrap_or_else`]: #method.unwrap_or_else
974
+ /// [`unwrap_or_default`]: #method.unwrap_or_default
941
975
///
942
976
/// # Panics
943
977
///
@@ -969,13 +1003,15 @@ impl<T, E: fmt::Debug> Result<T, E> {
969
1003
Err ( e) => unwrap_failed ( "called `Result::unwrap()` on an `Err` value" , & e) ,
970
1004
}
971
1005
}
1006
+ }
972
1007
973
- /// Unwraps a result, yielding the content of an [`Ok`].
1008
+ impl < T : fmt:: Debug , E > Result < T , E > {
1009
+ /// Returns the contained [`Err`] value, consuming the `self` value.
974
1010
///
975
1011
/// # Panics
976
1012
///
977
- /// Panics if the value is an [`Err `], with a panic message including the
978
- /// passed message, and the content of the [`Err `].
1013
+ /// Panics if the value is an [`Ok `], with a panic message including the
1014
+ /// passed message, and the content of the [`Ok `].
979
1015
///
980
1016
/// [`Ok`]: enum.Result.html#variant.Ok
981
1017
/// [`Err`]: enum.Result.html#variant.Err
@@ -985,22 +1021,20 @@ impl<T, E: fmt::Debug> Result<T, E> {
985
1021
/// Basic usage:
986
1022
///
987
1023
/// ```{.should_panic}
988
- /// let x: Result<u32, &str> = Err("emergency failure" );
989
- /// x.expect ("Testing expect "); // panics with `Testing expect: emergency failure `
1024
+ /// let x: Result<u32, &str> = Ok(10 );
1025
+ /// x.expect_err ("Testing expect_err "); // panics with `Testing expect_err: 10 `
990
1026
/// ```
991
1027
#[ inline]
992
1028
#[ track_caller]
993
- #[ stable( feature = "result_expect " , since = "1.4 .0" ) ]
994
- pub fn expect ( self , msg : & str ) -> T {
1029
+ #[ stable( feature = "result_expect_err " , since = "1.17 .0" ) ]
1030
+ pub fn expect_err ( self , msg : & str ) -> E {
995
1031
match self {
996
- Ok ( t) => t ,
997
- Err ( e) => unwrap_failed ( msg , & e ) ,
1032
+ Ok ( t) => unwrap_failed ( msg , & t ) ,
1033
+ Err ( e) => e ,
998
1034
}
999
1035
}
1000
- }
1001
1036
1002
- impl < T : fmt:: Debug , E > Result < T , E > {
1003
- /// Unwraps a result, yielding the content of an [`Err`].
1037
+ /// Returns the contained [`Err`] value, consuming the `self` value.
1004
1038
///
1005
1039
/// # Panics
1006
1040
///
@@ -1031,38 +1065,10 @@ impl<T: fmt::Debug, E> Result<T, E> {
1031
1065
Err ( e) => e,
1032
1066
}
1033
1067
}
1034
-
1035
- /// Unwraps a result, yielding the content of an [`Err`].
1036
- ///
1037
- /// # Panics
1038
- ///
1039
- /// Panics if the value is an [`Ok`], with a panic message including the
1040
- /// passed message, and the content of the [`Ok`].
1041
- ///
1042
- /// [`Ok`]: enum.Result.html#variant.Ok
1043
- /// [`Err`]: enum.Result.html#variant.Err
1044
- ///
1045
- /// # Examples
1046
- ///
1047
- /// Basic usage:
1048
- ///
1049
- /// ```{.should_panic}
1050
- /// let x: Result<u32, &str> = Ok(10);
1051
- /// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
1052
- /// ```
1053
- #[ inline]
1054
- #[ track_caller]
1055
- #[ stable( feature = "result_expect_err" , since = "1.17.0" ) ]
1056
- pub fn expect_err ( self , msg : & str ) -> E {
1057
- match self {
1058
- Ok ( t) => unwrap_failed ( msg, & t) ,
1059
- Err ( e) => e,
1060
- }
1061
- }
1062
1068
}
1063
1069
1064
1070
impl < T : Default , E > Result < T , E > {
1065
- /// Returns the contained value or a default
1071
+ /// Returns the contained [`Ok`] value or a default
1066
1072
///
1067
1073
/// Consumes the `self` argument then, if [`Ok`], returns the contained
1068
1074
/// value, otherwise if [`Err`], returns the default value for that
@@ -1101,7 +1107,7 @@ impl<T: Default, E> Result<T, E> {
1101
1107
1102
1108
#[ unstable( feature = "unwrap_infallible" , reason = "newly added" , issue = "61695" ) ]
1103
1109
impl < T , E : Into < !> > Result < T , E > {
1104
- /// Unwraps a result that can never be an [`Err`], yielding the content of the [`Ok`] .
1110
+ /// Returns the contained [`Ok`] value, but never panics .
1105
1111
///
1106
1112
/// Unlike [`unwrap`], this method is known to never panic on the
1107
1113
/// result types it is implemented for. Therefore, it can be used
0 commit comments