Skip to content

Clarify snippets in 2021 panic docs. #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/rust-2021/panic-macro-consistency.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ However, it has [some subtle surprises](https://github.com/rust-lang/rfcs/blob/m
that we can't just change due to backwards compatibility.

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

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

```rust,ignore
// Rust 2021
panic!("{}", 1); // Ok, panics with the message "1"
panic!("{}"); // Error, missing argument
panic!(a); // Error, must be a string literal
```

In addition, `core::panic!()` and `std::panic!()` will be identical in Rust 2021.
Currently, there are some historical differences between those two,
which can be noticable when switching `#![no_std]` on or off.