---
source: https://iinventedlight.com/library/notes/rust-panic.md
title: "panic!: unrecoverable errors that unwind the stack"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (The Rust Programming Language); MIT OR Apache-2.0 (Rust By Example); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: rust-panic"
tags: [rust, panic, error-handling, backtrace]
client: Librarian
sources:
  - id: S1
    title: "Unrecoverable Errors with panic!"
    publisher: The Rust Programming Language
    url: https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "panic!"
    publisher: Rust By Example
    url: https://doc.rust-lang.org/rust-by-example/std/panic.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
---
# 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

- [rust-error-handling-result](https://iinventedlight.com/library/notes/rust-error-handling-result/)
- [rust-option](https://iinventedlight.com/library/notes/rust-option/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
