Skip to content

Commit 72898ac

Browse files
committed
spicy
1 parent 00a315c commit 72898ac

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

library/core/src/result.rs

+43-25
Original file line numberDiff line numberDiff line change
@@ -1026,56 +1026,74 @@ impl<T, E> Result<T, E> {
10261026
///
10271027
/// # Common Message Styles
10281028
///
1029-
/// There are two common styles for how people word `expect` messages. Using the message to
1030-
/// present information to users encountering a panic ("expect as error message") or using the
1031-
/// message to present information to developers debugging the panic ("expect as
1032-
/// precondition").
1029+
/// There are two common styles for how people word `expect` messages. Using
1030+
/// the message to present information to users encountering a panic
1031+
/// ("expect as error message") or using the message to present information
1032+
/// to developers debugging the panic ("expect as precondition").
10331033
///
1034-
/// In the former case the expect message is used to describe the error that has occurred which
1035-
/// is considered a bug. Consider the following example:
1034+
/// In the former case the expect message is used to describe the error that
1035+
/// has occurred which is considered a bug. Consider the following example:
10361036
///
10371037
/// ```should_panic
10381038
/// // Read environment variable, panic if it is not present
10391039
/// let path = std::env::var("IMPORTANT_PATH").unwrap();
10401040
/// ```
10411041
///
1042-
/// In the "expect as error message" style we would use expect to describe that the environment
1043-
/// variable was not set when it should have been:
1042+
/// In the "expect as error message" style we would use expect to describe
1043+
/// that the environment variable was not set when it should have been:
10441044
///
10451045
/// ```should_panic
10461046
/// let path = std::env::var("IMPORTANT_PATH")
10471047
/// .expect("env variable `IMPORTANT_PATH` is not set");
10481048
/// ```
10491049
///
1050-
/// In the "expect as precondition" style, we would instead describe the reason we _expect_ the
1051-
/// `Result` will always be `Ok`. With this style we would prefer to write:
1050+
/// In the "expect as precondition" style, we would instead describe the
1051+
/// reason we _expect_ the `Result` should be `Ok`. With this style we would
1052+
/// prefer to write:
10521053
///
10531054
/// ```should_panic
10541055
/// let path = std::env::var("IMPORTANT_PATH")
1055-
/// .expect("env variable `IMPORTANT_PATH` is always set by `wrapper_script.sh`");
1056+
/// .expect("env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`");
10561057
/// ```
10571058
///
1058-
/// The "expect as error message" style has the advantage of giving a more user friendly error
1059-
/// message, and is more consistent with the default output of the [panic hook] provided by
1060-
/// `std`.
1059+
/// The "expect as error message" style does not work as well with the
1060+
/// default output of the std panic hooks, and often ends up repeating
1061+
/// information that is already communicated by the source error being
1062+
/// unwrapped:
10611063
///
10621064
/// ```text
10631065
/// thread 'main' panicked at 'env variable `IMPORTANT_PATH` is not set: NotPresent', src/main.rs:4:6
10641066
/// ```
10651067
///
1066-
/// The "expect as precondition" style instead focuses on source code readability, making it
1067-
/// easier to understand what must have gone wrong in situations where panics are being used to
1068-
/// represent bugs exclusively. But this extra information often looks confusing when presented
1069-
/// directly to users with the default `std` panic hook's report format:
1068+
/// In this example we end up mentioning that an env variable is not set,
1069+
/// followed by our source message that says the env is not present, the
1070+
/// only additional information we're communicating is the name of the
1071+
/// environment variable being checked.
10701072
///
1071-
/// ```text
1072-
/// thread 'main' panicked at 'env variable `IMPORTANT_PATH` is always set by `wrapper_script.sh`: NotPresent', src/main.rs:4:6
1073-
/// ```
1073+
/// The "expect as precondition" style instead focuses on source code
1074+
/// readability, making it easier to understand what must have gone wrong in
1075+
/// situations where panics are being used to represent bugs exclusively.
1076+
/// Also, by framing our expect in terms of what "SHOULD" have happened to
1077+
/// prevent the source error, we end up introducing new information that is
1078+
/// independent from our source error.
10741079
///
1075-
/// This style works best when paired with a custom [panic hook] like the one provided by the
1076-
/// CLI working group library, [`human-panic`], which dumps the panic messages to a crash
1077-
/// report file while showing users a more friendly "Oops, something went wrong!" message with
1078-
/// a suggestion to send the crash report file back to the developers.
1080+
/// ```text
1081+
/// thread 'main' panicked at 'env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`: NotPresent', src/main.rs:4:6
1082+
/// ```
1083+
///
1084+
/// In this example we are communicating not only the name of the
1085+
/// environment variable that should have been set, but also an explanation
1086+
/// for why it should have been set, and we let the source error display as
1087+
/// a clear contradiction to our expectation.
1088+
///
1089+
/// For programs where panics may be user facing, either style works best
1090+
/// when paired with a custom [panic hook] like the one provided by the CLI
1091+
/// working group library, [`human-panic`]. This panic hook dumps the panic
1092+
/// messages to a crash report file while showing users a more friendly
1093+
/// "Oops, something went wrong!" message with a suggestion to send the
1094+
/// crash report file back to the developers. Panic messages should be used
1095+
/// to represent bugs, and the information provided back is context intended
1096+
/// for the developer, not the user.
10791097
///
10801098
/// [panic hook]: https://doc.rust-lang.org/stable/std/panic/fn.set_hook.html
10811099
/// [`human-panic`]: https://docs.rs/human-panic

0 commit comments

Comments
 (0)