panic!: unrecoverable errors that unwind the stack
Answer: The panic! macro starts a panic and unwinds the stack. While unwinding, the runtime frees resources by calling destructors. Set the RUST_BACKTRACE environment variable to get a backtrace. Debug symbols are enabled by default for cargo build without --release.
When Rust panics
There are two ways to cause a panic: by taking an action that causes the code to panic, or by explicitly calling the panic! macro.
Indexing past the end of an array is one action that will panic.
Using [] is supposed to return an element. An invalid index leaves no element that would be correct.
A note tells us we can set the RUST_BACKTRACE environment variable to get a backtrace.
The backtrace lists all the functions that have been called to get to this point.
Debug symbols are on for cargo build and cargo run unless you pass --release.
Unwinding and cleanup
The panic! macro can be used to generate a panic and start unwinding its stack.
As the stack unwinds, the runtime calls destructors and frees the thread's resources.
In a one-thread program, the process prints the panic message and then exits.
See also
Claims
- id: C1 claim: A panic can come from a failing action or from calling panic!. source: S1 evidence: "explicitly calling the panic! macro"
- id: C2 claim: An invalid index causes a panic. source: S1 evidence: "accessing an array past the end"
- id: C3 claim: RUST_BACKTRACE requests a backtrace of the panic. source: S1 evidence: "RUST_BACKTRACE=1"
- id: C4 claim: Debug symbols are on by default without --release. source: S1 evidence: "without the --release flag"
- id: C5 claim: panic! starts unwinding the stack. source: S2 evidence: "start unwinding its stack"
- id: C6 claim: Unwinding runs destructors to free resources. source: S2 evidence: "calling the destructor of all its objects"
Sources
- [S1] Unrecoverable Errors with panic!. The Rust Programming Language. https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
- [S2] panic!. Rust By Example. https://doc.rust-lang.org/rust-by-example/std/panic.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Unrecoverable Errors with panic!; Rust By Example, panic!. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Unrecoverable Errors with panic!; Rust By Example, panic!. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "panic!: unrecoverable errors that unwind the stack." iinventedlight Library, iil:note:dnr8pcew, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-panic/. License: CC-BY-4.0.
@misc{iil_note_dnr8pcew,
author = {{iinventedlight}},
title = {{panic!: unrecoverable errors that unwind the stack}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/rust-panic/},
note = {Permanent ID iil:note:dnr8pcew. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-panic.schema.json (schema; CC-BY-4.0)
- rust-panic.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-panic.md.