Panic handlers and startup
Quick answer
Bare-metal programs need a startup path and a panic policy because an operating system is not available to supply the usual process runtime.
From reset to application
Startup code normally establishes the stack, prepares initialized data and zeroes the required memory sections before calling application code. A linker script describes where sections belong in flash and RAM. A panic handler returns the never type !; it must not return to the failing code. The following handler is a fragment, not a complete firmware image. A real fault policy may need a watchdog or safe hardware state.
#![no_std]use core::panic::PanicInfo;#[panic_handler]fn panic(_info: &PanicInfo<'_>) -> ! { loop { core::hint::spin_loop(); }}Manual local Rust/Cargo candidate; not executed. Expected behavior:
Fragment for final no_std firmware; requires target startup and linker setup. Never returns.Spinning is a teaching policy only; it does not guarantee safe hardware behavior.
fn fits(data: usize, bss: usize, stack: usize, ram: usize) -> bool { data.checked_add(bss).and_then(|n| n.checked_add(stack)).is_some_and(|n| n <= ram)}fn main() { println!("{} {}", fits(512, 1024, 512, 4096), fits(2048, 2048, 512, 4096)); }Expected stdout (not executed):
true falseThis estimate ignores padding and other reservations; inspect the actual link map.
Common mistake
Do not link a second panic handler into an application that already has one.
Try it yourself
Add a heap reservation to fits and test the exact boundary.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Panic handlers and startup — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A no_std library passes cargo check. Is it now a complete bootable firmware image?
Panic handlers and startup — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust #![no_std] use core::panic::PanicInfo; #[panic_handler] fn panic(_info: &PanicInfo<'_>) -> ! { loop { core::hint::spin_loop(); } } ``` Which outcome or interpretation best matches this example?
Panic handlers and startup — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn fits(data: usize, bss: usize, stack: usize, ram: usize) -> bool { data.checked_add(bss).and_then(|n| n.checked_add(stack)).is_some_and(|n| n <= ram) } fn main() { println!("{} {}", fits(512, 1024, 512, 4096), fits(2048, 2048, 512, 4096)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.