@@ -8,8 +8,8 @@ tests. `panic!` is closely tied with the `unwrap` method of both
88[ ` Option ` ] [ ounwrap ] and [ ` Result ` ] [ runwrap ] enums. Both implementations call
99` panic! ` when they are set to [ ` None ` ] or [ ` Err ` ] variants.
1010
11- When using ` panic!() ` you can specify a string payload, that is built using
12- the [ ` format! ` syntax] . That payload is used when injecting the panic into
11+ When using ` panic!() ` you can specify a string payload that is built using
12+ [ formatting syntax] . That payload is used when injecting the panic into
1313the calling Rust thread, causing the thread to panic entirely.
1414
1515The behavior of the default ` std ` hook, i.e. the code that runs directly
@@ -18,6 +18,7 @@ after the panic is invoked, is to print the message payload to
1818call. You can override the panic hook using [ ` std::panic::set_hook() ` ] .
1919Inside the hook a panic can be accessed as a ` &dyn Any + Send ` ,
2020which contains either a ` &str ` or ` String ` for regular ` panic!() ` invocations.
21+ (Whether a particular invocation contains the payload at type ` &str ` or ` String ` is unspecified and can change.)
2122To panic with a value of another other type, [ ` panic_any ` ] can be used.
2223
2324See also the macro [ ` compile_error! ` ] , for raising errors during compilation.
@@ -66,22 +67,26 @@ program with code `101`.
6667
6768# Editions
6869
69- In Rust Editions prior to 2021, ` std::panic!(x) ` with a single
70- argument is equivalent to
71- [ ` std::panic::panic_any(x) ` ] ( ../std/panic/fn.panic_any.html ) .
72- This is true even if the argument is a string literal.
70+ Behavior of the panic macros changed over editions.
7371
74- For example, in Rust 2015 ` panic!("problem: {reason}") ` panics with a
75- payload of literally ` "problem: {reason}" ` (a ` &'static str ` ), which
76- is probably not what was intended. In current Rust this usage
77- captures and formats a variable ` reason ` from the surrounding scope.
78-
79- In Rust editions prior to 2021, ` core::panic!(x) ` requires that
80- ` x ` be ` &str ` , but does not require it to be a literal. In Rust 2021,
81- these cases must be written ` panic!("{}", x) ` .
72+ ## 2021 and later
8273
8374In Rust 2021 and later, ` panic! ` always requires a format string and
8475the applicable format arguments, and is the same in ` core ` and ` std ` .
76+ Use [ ` std::panic::panic_any(x) ` ] ( ../std/panic/fn.panic_any.html ) to
77+ panic with an arbitrary payload.
78+
79+ ## 2018 and 2015
80+
81+ In Rust Editions prior to 2021, ` std::panic!(x) ` with a single
82+ argument directly uses that argument as a payload.
83+ This is true even if the argument is a string literal.
84+ For example, ` panic!("problem: {reason}") ` panics with a
85+ payload of literally ` "problem: {reason}" ` (a ` &'static str ` ).
86+
87+ ` core::panic!(x) ` with a single argument requires that ` x ` be ` &str ` ,
88+ but otherwise behaves like ` std::panic! ` . In particular, the string
89+ need not be a literal, and is not interpreted as a format string.
8590
8691# Examples
8792
0 commit comments