Skip to content

Commit 5005272

Browse files
committed
Move the panic_handler example to the intro and an example block
1 parent 9571d4d commit 5005272

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

src/panic.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,48 @@ r[panic.panic_handler]
2222
r[panic.panic_handler.intro]
2323
The *`panic_handler` attribute* can be applied to a function to define the behavior of panics.
2424

25-
r[panic.panic_handler.allowed-positions]
26-
The `panic_handler` attribute can only be applied to a function with signature `fn(&PanicInfo) -> !`.
25+
> [!EXAMPLE]
26+
> Below is shown a `panic_handler` function that logs the panic message and then halts the thread.
27+
> <!-- ignore: test infrastructure can't handle no_std -->
28+
> ```rust,ignore
29+
> #![no_std]
30+
>
31+
> use core::fmt::{self, Write};
32+
> use core::panic::PanicInfo;
33+
>
34+
> struct Sink {
35+
> // ..
36+
> # _0: (),
37+
> }
38+
> #
39+
> # impl Sink {
40+
> # fn new() -> Sink { Sink { _0: () }}
41+
> # }
42+
> #
43+
> # impl fmt::Write for Sink {
44+
> # fn write_str(&mut self, _: &str) -> fmt::Result { Ok(()) }
45+
> # }
46+
>
47+
> #[panic_handler]
48+
> fn panic(info: &PanicInfo) -> ! {
49+
> let mut sink = Sink::new();
50+
>
51+
> // logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
52+
> let _ = writeln!(sink, "{}", info);
53+
>
54+
> loop {}
55+
> }
56+
> ```
2757
2858
> [!NOTE]
2959
> The [`PanicInfo`] struct contains information about the location of the panic.
3060
61+
r[panic.panic_handler.allowed-positions]
62+
The `panic_handler` attribute can only be applied to a function with signature `fn(&PanicInfo) -> !`.
63+
3164
r[panic.panic_handler.unique]
3265
There must be a single `panic_handler` function in the dependency graph.
3366
34-
Below is shown a `panic_handler` function that logs the panic message and then halts the thread.
35-
36-
<!-- ignore: test infrastructure can't handle no_std -->
37-
```rust,ignore
38-
#![no_std]
39-
40-
use core::fmt::{self, Write};
41-
use core::panic::PanicInfo;
42-
43-
struct Sink {
44-
// ..
45-
# _0: (),
46-
}
47-
#
48-
# impl Sink {
49-
# fn new() -> Sink { Sink { _0: () }}
50-
# }
51-
#
52-
# impl fmt::Write for Sink {
53-
# fn write_str(&mut self, _: &str) -> fmt::Result { Ok(()) }
54-
# }
55-
56-
#[panic_handler]
57-
fn panic(info: &PanicInfo) -> ! {
58-
let mut sink = Sink::new();
59-
60-
// logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
61-
let _ = writeln!(sink, "{}", info);
62-
63-
loop {}
64-
}
65-
```
66-
6767
r[panic.panic_handler.std]
6868
### Standard behavior
6969

0 commit comments

Comments
 (0)