Skip to content

Commit 4413c52

Browse files
committed
Centralize panic macro documentation
1 parent 0e9b465 commit 4413c52

File tree

3 files changed

+49
-50
lines changed

3 files changed

+49
-50
lines changed

src/libcore/macros.rs src/libcore/macros/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/// Panics the current thread.
2-
///
3-
/// For details, see `std::macros`.
1+
#[doc(include = "panic.md")]
42
#[macro_export]
53
#[allow_internal_unstable(core_panic, __rust_unstable_column)]
64
#[stable(feature = "core", since = "1.6.0")]

src/libcore/macros/panic.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Panics the current thread.
2+
3+
This allows a program to terminate immediately and provide feedback
4+
to the caller of the program. `panic!` should be used when a program reaches
5+
an unrecoverable state.
6+
7+
This macro is the perfect way to assert conditions in example code and in
8+
tests. `panic!` is closely tied with the `unwrap` method of both [`Option`]
9+
and [`Result`][runwrap] enums. Both implementations call `panic!` when they are set
10+
to None or Err variants.
11+
12+
This macro is used to inject panic into a Rust thread, causing the thread to
13+
panic entirely. Each thread's panic can be reaped as the `Box<Any>` type,
14+
and the single-argument form of the `panic!` macro will be the value which
15+
is transmitted.
16+
17+
[`Result`] enum is often a better solution for recovering from errors than
18+
using the `panic!` macro. This macro should be used to avoid proceeding using
19+
incorrect values, such as from external sources. Detailed information about
20+
error handling is found in the [book].
21+
22+
The multi-argument form of this macro panics with a string and has the
23+
[`format!`] syntax for building a string.
24+
25+
See also the macro [`compile_error!`], for raising errors during compilation.
26+
27+
[runwrap]: ../std/result/enum.Result.html#method.unwrap
28+
[`Option`]: ../std/option/enum.Option.html#method.unwrap
29+
[`Result`]: ../std/result/enum.Result.html
30+
[`format!`]: ../std/macro.format.html
31+
[`compile_error!`]: ../std/macro.compile_error.html
32+
[book]: ../book/ch09-00-error-handling.html
33+
34+
# Current implementation
35+
36+
If the main thread panics it will terminate all your threads and end your
37+
program with code `101`.
38+
39+
# Examples
40+
41+
```should_panic
42+
# #![allow(unreachable_code)]
43+
panic!();
44+
panic!("this is a terrible mistake!");
45+
panic!(4); // panic with the value of 4 to be collected elsewhere
46+
panic!("this is a {} {message}", "fancy", message = "message");
47+
```

src/libstd/macros.rs

+1-47
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,7 @@
44
//! library. Each macro is available for use when linking against the standard
55
//! library.
66
7-
/// Panics the current thread.
8-
///
9-
/// This allows a program to terminate immediately and provide feedback
10-
/// to the caller of the program. `panic!` should be used when a program reaches
11-
/// an unrecoverable state.
12-
///
13-
/// This macro is the perfect way to assert conditions in example code and in
14-
/// tests. `panic!` is closely tied with the `unwrap` method of both [`Option`]
15-
/// and [`Result`][runwrap] enums. Both implementations call `panic!` when they are set
16-
/// to None or Err variants.
17-
///
18-
/// This macro is used to inject panic into a Rust thread, causing the thread to
19-
/// panic entirely. Each thread's panic can be reaped as the `Box<Any>` type,
20-
/// and the single-argument form of the `panic!` macro will be the value which
21-
/// is transmitted.
22-
///
23-
/// [`Result`] enum is often a better solution for recovering from errors than
24-
/// using the `panic!` macro. This macro should be used to avoid proceeding using
25-
/// incorrect values, such as from external sources. Detailed information about
26-
/// error handling is found in the [book].
27-
///
28-
/// The multi-argument form of this macro panics with a string and has the
29-
/// [`format!`] syntax for building a string.
30-
///
31-
/// See also the macro [`compile_error!`], for raising errors during compilation.
32-
///
33-
/// [runwrap]: ../std/result/enum.Result.html#method.unwrap
34-
/// [`Option`]: ../std/option/enum.Option.html#method.unwrap
35-
/// [`Result`]: ../std/result/enum.Result.html
36-
/// [`format!`]: ../std/macro.format.html
37-
/// [`compile_error!`]: ../std/macro.compile_error.html
38-
/// [book]: ../book/ch09-00-error-handling.html
39-
///
40-
/// # Current implementation
41-
///
42-
/// If the main thread panics it will terminate all your threads and end your
43-
/// program with code `101`.
44-
///
45-
/// # Examples
46-
///
47-
/// ```should_panic
48-
/// # #![allow(unreachable_code)]
49-
/// panic!();
50-
/// panic!("this is a terrible mistake!");
51-
/// panic!(4); // panic with the value of 4 to be collected elsewhere
52-
/// panic!("this is a {} {message}", "fancy", message = "message");
53-
/// ```
7+
#[doc(include = "../libcore/macros/panic.md")]
548
#[macro_export]
559
#[stable(feature = "rust1", since = "1.0.0")]
5610
#[allow_internal_unstable(__rust_unstable_column, libstd_sys_internals)]

0 commit comments

Comments
 (0)