forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralize panic macro documentation
- Loading branch information
Showing
3 changed files
with
49 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Panics the current thread. | ||
|
||
This allows a program to terminate immediately and provide feedback | ||
to the caller of the program. `panic!` should be used when a program reaches | ||
an unrecoverable state. | ||
|
||
This macro is the perfect way to assert conditions in example code and in | ||
tests. `panic!` is closely tied with the `unwrap` method of both [`Option`] | ||
and [`Result`][runwrap] enums. Both implementations call `panic!` when they are set | ||
to None or Err variants. | ||
|
||
This macro is used to inject panic into a Rust thread, causing the thread to | ||
panic entirely. Each thread's panic can be reaped as the `Box<Any>` type, | ||
and the single-argument form of the `panic!` macro will be the value which | ||
is transmitted. | ||
|
||
[`Result`] enum is often a better solution for recovering from errors than | ||
using the `panic!` macro. This macro should be used to avoid proceeding using | ||
incorrect values, such as from external sources. Detailed information about | ||
error handling is found in the [book]. | ||
|
||
The multi-argument form of this macro panics with a string and has the | ||
[`format!`] syntax for building a string. | ||
|
||
See also the macro [`compile_error!`], for raising errors during compilation. | ||
|
||
[runwrap]: ../std/result/enum.Result.html#method.unwrap | ||
[`Option`]: ../std/option/enum.Option.html#method.unwrap | ||
[`Result`]: ../std/result/enum.Result.html | ||
[`format!`]: ../std/macro.format.html | ||
[`compile_error!`]: ../std/macro.compile_error.html | ||
[book]: ../book/ch09-00-error-handling.html | ||
|
||
# Current implementation | ||
|
||
If the main thread panics it will terminate all your threads and end your | ||
program with code `101`. | ||
|
||
# Examples | ||
|
||
```should_panic | ||
# #![allow(unreachable_code)] | ||
panic!(); | ||
panic!("this is a terrible mistake!"); | ||
panic!(4); // panic with the value of 4 to be collected elsewhere | ||
panic!("this is a {} {message}", "fancy", message = "message"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters