Skip to content

Commit 93f140e

Browse files
committed
panic_implementation -> panic_handler; beneath std -> the Rust runtime
1 parent 2c9b97d commit 93f140e

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
- [Behavior considered undefined](behavior-considered-undefined.md)
8888
- [Behavior not considered unsafe](behavior-not-considered-unsafe.md)
8989

90-
- [Beneath `std`](beneath-std.md)
90+
- [The Rust runtime](runtime.md)
9191

9292
[Appendix: Influences](influences.md)
9393

src/beneath-std.md

-39
This file was deleted.

src/runtime.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# The Rust runtime
2+
3+
This section documents features that define some aspects of the Rust runtime. A list of such
4+
features is shown below:
5+
6+
- `#[panic_handler]`, the panicking behavior
7+
8+
## `#[panic_handler]`
9+
10+
The `panic_handler` attribute can only be applied to a function with signature
11+
`fn(&PanicInfo) -> !`. The function marked with this attribute defines the behavior of panics. The
12+
[`PanicInfo`] struct contains information about the location of the panic. There must be a single
13+
`panic_handler` function in the dependency graph of a binary, dylib or cdylib crate.
14+
15+
[`PanicInfo`]: https://doc.rust-lang.org/nightly/core/panic/struct.PanicInfo.html
16+
17+
Below is shown a `panic_handler` function that logs the panic message and then halts the
18+
thread.
19+
20+
``` rust
21+
#![no_std]
22+
23+
use core::intrinsics;
24+
use core::panic::PanicInfo;
25+
26+
#[panic_handler]
27+
fn panic(info: &PanicInfo) -> ! {
28+
let mut sink = /* .. */;
29+
30+
// logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
31+
let _ = writeln!(sink, "{}", info);
32+
33+
loop {}
34+
}
35+
```
36+
37+
### Standard behavior
38+
39+
The standard library provides an implementation of `panic_handler` than can be
40+
statically customized using the `-C panic` flag. `-C panic=abort` makes panics
41+
abort the process, and `-C panic=unwind` makes panics unwind the panicking
42+
thread. If no panicking behavior is specified using `-C panic` one of these two
43+
behaviors is chosen according to the compilation target.

0 commit comments

Comments
 (0)