1
- ## #[ panic_implementation ]
1
+ ## #[ panic_handler ]
2
2
3
- ` #[panic_implementation ] ` is used to define the behavior of ` panic! ` in ` #![no_std] ` applications.
4
- The ` #[panic_implementation ] ` attribute must be applied to a function with signature `fn(&PanicInfo)
3
+ ` #[panic_handler ] ` is used to define the behavior of ` panic! ` in ` #![no_std] ` applications.
4
+ The ` #[panic_handler ] ` attribute must be applied to a function with signature `fn(&PanicInfo)
5
5
-> !` and such function must appear * once* in the dependency graph of a binary / dylib / cdylib
6
6
crate. The API of ` PanicInfo ` can be found in the [ API docs] .
7
7
8
8
[ API docs ] : https://doc.rust-lang.org/nightly/core/panic/struct.PanicInfo.html
9
9
10
10
Given that ` #![no_std] ` applications have no * standard* output and that some ` #![no_std] `
11
11
applications, e.g. embedded applications, need different panicking behaviors for development and for
12
- release it can be helpful to have panic crates, crate that only contain a ` #[panic_implementation ] ` .
12
+ release it can be helpful to have panic crates, crate that only contain a ` #[panic_handler ] ` .
13
13
This way applications can easily swap the panicking behavior by simply linking to a different panic
14
14
crate.
15
15
@@ -20,32 +20,27 @@ whether is compiled using the dev profile (`cargo build`) or using the release p
20
20
``` rust
21
21
// crate: panic-semihosting -- log panic message to the host stderr using semihosting
22
22
23
- #![feature(core_intrinsics)]
24
- #![feature(panic_implementation)]
25
23
#![no_std]
26
24
27
- #[panic_implementation ]
25
+ #[panic_handler ]
28
26
fn panic (info : & PanicInfo ) -> ! {
29
27
let host_stderr = /* .. */ ;
30
28
29
+ // logs "panicked at '$reason', src/main.rs:27:4" to the host stderr
31
30
writeln! (host_stderr , " {}" , info ). ok ();
32
31
33
- core :: intrinsics :: breakpoint ();
34
-
35
32
loop {}
36
33
}
37
34
```
38
35
39
36
``` rust
40
- // crate: panic-abort -- abort on panic!
37
+ // crate: panic-halt -- halt the thread on panic; messages are discarded
41
38
42
- #![feature(core_intrinsics)]
43
- #![feature(panic_implementation)]
44
39
#![no_std]
45
40
46
- #[panic_implementation ]
41
+ #[panic_handler ]
47
42
fn panic (info : & PanicInfo ) -> ! {
48
- unsafe { core :: intrinsics :: abort () }
43
+ loop { }
49
44
}
50
45
```
51
46
@@ -60,7 +55,7 @@ extern crate panic_semihosting;
60
55
61
56
// release profile
62
57
#[cfg(not(debug_assertions))]
63
- extern crate panic_abort ;
58
+ extern crate panic_halt ;
64
59
65
60
// omitted: other `extern crate`s
66
61
0 commit comments