Skip to content

Commit e5d7636

Browse files
authored
Merge pull request #245 from m-ou-se/clarify-panic-snippets
Clarify snippets in 2021 panic docs.
2 parents c74b2a0 + cd9bafc commit e5d7636

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/rust-2021/panic-macro-consistency.md

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ However, it has [some subtle surprises](https://github.com/rust-lang/rfcs/blob/m
1414
that we can't just change due to backwards compatibility.
1515

1616
```rust,ignore
17+
// Rust 2018
1718
panic!("{}", 1); // Ok, panics with the message "1"
1819
panic!("{}"); // Ok, panics with the message "{}"
1920
```
@@ -22,6 +23,7 @@ The `panic!()` macro only uses string formatting when it's invoked with more tha
2223
When invoked with a single argument, it doesn't even look at that argument.
2324

2425
```rust,ignore
26+
// Rust 2018
2527
let a = "{";
2628
println!(a); // Error: First argument must be a format string literal
2729
panic!(a); // Ok: The panic macro doesn't care
@@ -43,6 +45,13 @@ Since `panic!()` will no longer accept arbitrary payloads,
4345
[`panic_any()`](https://doc.rust-lang.org/stable/std/panic/fn.panic_any.html)
4446
will be the only way to panic with something other than a formatted string.
4547

48+
```rust,ignore
49+
// Rust 2021
50+
panic!("{}", 1); // Ok, panics with the message "1"
51+
panic!("{}"); // Error, missing argument
52+
panic!(a); // Error, must be a string literal
53+
```
54+
4655
In addition, `core::panic!()` and `std::panic!()` will be identical in Rust 2021.
4756
Currently, there are some historical differences between those two,
4857
which can be noticable when switching `#![no_std]` on or off.

0 commit comments

Comments
 (0)