expression
Declare a panic handler for a freestanding binary
A freestanding no_std executable that does not link std needs a panic implementation supplied by the program or one dependency. The handler never returns. This minimal handler spins indefinitely and is intentionally not suitable for real failure recovery; device-specific safe shutdown, diagnostics and watchdog policy must be designed separately. The fragment has no reset entry or linker script, so only compile-to-metadata checking is proposed. No runtime output expected: compile-only candidate, not linked firmware. Common mistake: Defining multiple panic handlers in the final link graph, or treating a spin loop as a universal safe device state.
Formal Syntax & Structure
rust syntaxrust
#[panic_handler] fn panic(info: &core::panic::PanicInfo) -> !Working Example
Example Snippetrust
#![no_std]#![no_main]use core::panic::PanicInfo;#[panic_handler]fn on_panic(_info: &PanicInfo<'_>) -> ! { loop { core::hint::spin_loop(); }}