惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
P
Proofpoint News Feed
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
GbyAI
GbyAI
博客园 - 聂微东
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
美团技术团队
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
U
Unit 42
博客园 - 叶小钗
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
MongoDB | Blog
MongoDB | Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
[Rust Guide] 9.1. Unrecoverable Errors and Panic!
SomeB1oody · 2026-04-26 · via DEV Community

If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.

9.1.1 Rust Error Handling Overview

Rust is extremely reliable, and that reliability extends to error handling. In most cases, Rust forces you to think about where errors might occur and then ensures at compile time that they are handled properly.

In Rust, errors are divided into two broad categories:

  • Recoverable errors: for example, a file not being found. In that case, you can pass the error message to the user and let the user try again.
  • Unrecoverable errors: another way to say “bug”, for example, an out-of-bounds index.

Most other programming languages do not make this distinction deliberately. They usually handle both through a single mechanism such as exceptions. Rust does not have a similar exception mechanism.

  • For recoverable errors, Rust provides the Result<T, E> type, which will be covered in the next article.
  • For unrecoverable errors, Rust provides the panic! macro. When this macro is executed, the program immediately stops running.

9.1.2 panic!

Sometimes something terrible happens in code, and the developer has no real way to deal with it. To handle this situation, Rust provides the panic! macro.

When this macro runs, the following happens:

  • It prints an error message.
  • Then it unwinds and cleans up the call stack.
  • It exits the program.

9.1.3 When panic! Happens: Unwinding or Aborting the Call Stack

Unwinding the call stack does a lot of work, because Rust walks back through the stack and cleans up data from every function it encounters along the way.

By contrast, Rust also offers the option to abort the call stack. This means no cleanup is performed; the program stops immediately, and the memory used by the program is left for the operating system to clean up later.

If you want a smaller binary, change the setting from “unwind” to “abort”: set panic = "abort" in the appropriate profile section of Cargo.toml.

Here is my Cargo.toml as an example:

[package]
name = "RustStudy"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.5"

[profile.release]
panic = "abort"

Enter fullscreen mode Exit fullscreen mode

profile.release means running in release mode.

9.1.4 The panic! Macro

Let’s look at an example of the panic! macro:

fn main() {
    panic!("Something went wrong");
}

Enter fullscreen mode Exit fullscreen mode

This is a very simple example. The argument to the panic! macro is the error message, and it will be printed when the program stops.

Output:

thread 'main' panicked at src/main.rs:2:5:
Something went wrong
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:74:14
   2: RustStudy::main
             at ./src/main.rs:2:5
   3: core::ops::function::FnOnce::call_once
             at /Users/stanyin/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Enter fullscreen mode Exit fullscreen mode

In the earlier articles, the program also panicked, but I did not paste the stack backtrace into the article then because we had not covered it yet. What you see above is the complete panic information. Now let’s break it down:

  • The first line tells you where the panic occurred — line 2, column 5 of main.rs in the src directory.
  • The second line is the error message defined by the program.
  • Starting from the third line, the stack backtrace is the backtrace information. At the position labeled 2 is main.rs. The backtrace contains the list of all functions that were called to reach the place where the error occurred, and below that — at position 3 — is the code that called our code, which may include Rust’s core library, the standard library, or third-party libraries.
  • The final note line says you can set RUST_BACKTRACE to full to get all the detailed information. On Windows, type set RUST_BACKTRACE=full && cargo run in the terminal. On macOS/Linux, type export RUST_BACKTRACE=full && cargo run.

To obtain debugging information like this, there is one more prerequisite: the program must be running in debug mode rather than release mode (--release). cargo build and cargo run use debug mode by default, so just make sure not to pass the --release flag.