@@ -1026,56 +1026,74 @@ impl<T, E> Result<T, E> {
1026
1026
///
1027
1027
/// # Common Message Styles
1028
1028
///
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").
1033
1033
///
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:
1036
1036
///
1037
1037
/// ```should_panic
1038
1038
/// // Read environment variable, panic if it is not present
1039
1039
/// let path = std::env::var("IMPORTANT_PATH").unwrap();
1040
1040
/// ```
1041
1041
///
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:
1044
1044
///
1045
1045
/// ```should_panic
1046
1046
/// let path = std::env::var("IMPORTANT_PATH")
1047
1047
/// .expect("env variable `IMPORTANT_PATH` is not set");
1048
1048
/// ```
1049
1049
///
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:
1052
1053
///
1053
1054
/// ```should_panic
1054
1055
/// 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`");
1056
1057
/// ```
1057
1058
///
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:
1061
1063
///
1062
1064
/// ```text
1063
1065
/// thread 'main' panicked at 'env variable `IMPORTANT_PATH` is not set: NotPresent', src/main.rs:4:6
1064
1066
/// ```
1065
1067
///
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.
1070
1072
///
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.
1074
1079
///
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.
1079
1097
///
1080
1098
/// [panic hook]: https://doc.rust-lang.org/stable/std/panic/fn.set_hook.html
1081
1099
/// [`human-panic`]: https://docs.rs/human-panic
0 commit comments