@@ -798,8 +798,7 @@ impl<T, E> Result<T, E> {
798798 }
799799 }
800800
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.
803802 ///
804803 /// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
805804 /// the result of a function call, it is recommended to use [`unwrap_or_else`],
@@ -814,27 +813,25 @@ impl<T, E> Result<T, E> {
814813 /// Basic usage:
815814 ///
816815 /// ```
817- /// let optb = 2;
816+ /// let default = 2;
818817 /// let x: Result<u32, &str> = Ok(9);
819- /// assert_eq!(x.unwrap_or(optb ), 9);
818+ /// assert_eq!(x.unwrap_or(default ), 9);
820819 ///
821820 /// let x: Result<u32, &str> = Err("error");
822- /// assert_eq!(x.unwrap_or(optb ), optb );
821+ /// assert_eq!(x.unwrap_or(default ), default );
823822 /// ```
824823 #[ inline]
825824 #[ 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 {
827826 match self {
828827 Ok ( t) => t,
829- Err ( _) => optb ,
828+ Err ( _) => default ,
830829 }
831830 }
832831
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.
835833 ///
836834 /// [`Ok`]: enum.Result.html#variant.Ok
837- /// [`Err`]: enum.Result.html#variant.Err
838835 ///
839836 /// # Examples
840837 ///
@@ -937,7 +934,44 @@ impl<T: Clone, E> Result<&mut T, E> {
937934}
938935
939936impl < 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
941975 ///
942976 /// # Panics
943977 ///
@@ -969,13 +1003,15 @@ impl<T, E: fmt::Debug> Result<T, E> {
9691003 Err ( e) => unwrap_failed ( "called `Result::unwrap()` on an `Err` value" , & e) ,
9701004 }
9711005 }
1006+ }
9721007
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.
9741010 ///
9751011 /// # Panics
9761012 ///
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 `].
9791015 ///
9801016 /// [`Ok`]: enum.Result.html#variant.Ok
9811017 /// [`Err`]: enum.Result.html#variant.Err
@@ -985,22 +1021,20 @@ impl<T, E: fmt::Debug> Result<T, E> {
9851021 /// Basic usage:
9861022 ///
9871023 /// ```{.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 `
9901026 /// ```
9911027 #[ inline]
9921028 #[ 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 {
9951031 match self {
996- Ok ( t) => t ,
997- Err ( e) => unwrap_failed ( msg , & e ) ,
1032+ Ok ( t) => unwrap_failed ( msg , & t ) ,
1033+ Err ( e) => e ,
9981034 }
9991035 }
1000- }
10011036
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.
10041038 ///
10051039 /// # Panics
10061040 ///
@@ -1031,38 +1065,10 @@ impl<T: fmt::Debug, E> Result<T, E> {
10311065 Err ( e) => e,
10321066 }
10331067 }
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- }
10621068}
10631069
10641070impl < T : Default , E > Result < T , E > {
1065- /// Returns the contained value or a default
1071+ /// Returns the contained [`Ok`] value or a default
10661072 ///
10671073 /// Consumes the `self` argument then, if [`Ok`], returns the contained
10681074 /// value, otherwise if [`Err`], returns the default value for that
@@ -1101,7 +1107,7 @@ impl<T: Default, E> Result<T, E> {
11011107
11021108#[ unstable( feature = "unwrap_infallible" , reason = "newly added" , issue = "61695" ) ]
11031109impl < 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 .
11051111 ///
11061112 /// Unlike [`unwrap`], this method is known to never panic on the
11071113 /// result types it is implemented for. Therefore, it can be used
0 commit comments