Skip to content

Commit 5df3b98

Browse files
authored
Rollup merge of rust-lang#99579 - CleanCut:expect-warning, r=joshtriplett
Add same warning to Result::expect as Result::unwrap I was reading a recent blog post by Jimmy Hartzell and [he noted](https://www.thecodedmessage.com/posts/2022-07-14-programming-unwrap/#context): > I will however note that the documentation of `unwrap` comes with [a warning not to use it](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap). The warning is framed in terms of the fact that `unwrap` may panic, but the [documentation of `expect`](https://doc.rust-lang.org/std/result/enum.Result.html#method.expect), where this is equally true, does not come with such a warning. It _is_ equally true. Let's add the same warning to `expect`. This PR is a copy-and-paste of the warning text from the docstring for `unwrap`.
2 parents 92bebac + 7ba0be8 commit 5df3b98

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

library/core/src/result.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,15 @@ impl<T, E> Result<T, E> {
10091009

10101010
/// Returns the contained [`Ok`] value, consuming the `self` value.
10111011
///
1012+
/// Because this function may panic, its use is generally discouraged.
1013+
/// Instead, prefer to use pattern matching and handle the [`Err`]
1014+
/// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
1015+
/// [`unwrap_or_default`].
1016+
///
1017+
/// [`unwrap_or`]: Result::unwrap_or
1018+
/// [`unwrap_or_else`]: Result::unwrap_or_else
1019+
/// [`unwrap_or_default`]: Result::unwrap_or_default
1020+
///
10121021
/// # Panics
10131022
///
10141023
/// Panics if the value is an [`Err`], with a panic message including the

0 commit comments

Comments
 (0)