Skip to content

Commit 1116707

Browse files
ijacksonRalfJung
authored andcommitted
panic macro: Document edition differences
Having a section for this inspired by the docs for array::IntoIterator
1 parent 7e02fd8 commit 1116707

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

library/core/src/macros/panic.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ For more detailed information about error handling check out the [book] or the
6464
If the main thread panics it will terminate all your threads and end your
6565
program with code `101`.
6666

67+
# Editions
68+
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.
73+
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)`.
82+
83+
In Rust 2021 and later, `panic!` always requires a format string and
84+
the applicable format arguments, and is the same in `core` and `std`.
85+
6786
# Examples
6887

6988
```should_panic

0 commit comments

Comments
 (0)