@@ -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.
@@ -55,7 +56,7 @@ For more detailed information about error handling check out the [book] or the
55
56
[ `panic_any` ] : ../std/panic/fn.panic_any.html
56
57
[ `Box` ] : ../std/boxed/struct.Box.html
57
58
[ `Any` ] : crate::any::Any
58
- [ `format!` ] : ../std/macro.format .html
59
+ [ `format!` syntax ] : ../std/fmt/index .html
59
60
[ book ] : ../book/ch09-00-error-handling.html
60
61
[ `std::result` ] : ../std/result/index.html
61
62
@@ -64,6 +65,29 @@ For more detailed information about error handling check out the [book] or the
64
65
If the main thread panics it will terminate all your threads and end your
65
66
program with code ` 101 ` .
66
67
68
+ # Editions
69
+
70
+ Behavior of the panic macros changed over editions.
71
+
72
+ ## 2021 and later
73
+
74
+ In Rust 2021 and later, ` panic! ` always requires a format string and
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.
90
+
67
91
# Examples
68
92
69
93
``` should_panic
0 commit comments