@@ -8,8 +8,8 @@ tests. `panic!` is closely tied with the `unwrap` method of both
8
8
[ ` Option ` ] [ ounwrap ] and [ ` Result ` ] [ runwrap ] enums. Both implementations call
9
9
` panic! ` when they are set to [ ` None ` ] or [ ` Err ` ] variants.
10
10
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
13
13
the calling Rust thread, causing the thread to panic entirely.
14
14
15
15
The 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
18
18
call. You can override the panic hook using [ ` std::panic::set_hook() ` ] .
19
19
Inside the hook a panic can be accessed as a ` &dyn Any + Send ` ,
20
20
which 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.)
21
22
To panic with a value of another other type, [ ` panic_any ` ] can be used.
22
23
23
24
See also the macro [ ` compile_error! ` ] , for raising errors during compilation.
@@ -66,22 +67,26 @@ program with code `101`.
66
67
67
68
# Editions
68
69
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.
73
71
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
82
73
83
74
In Rust 2021 and later, ` panic! ` always requires a format string and
84
75
the 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.
85
90
86
91
# Examples
87
92
0 commit comments