← All notes

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

Sources

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

Permanent ID: iil:note:dnr8pcew

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.},
}

Markdown

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.

All artifact files (JSON index)

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.