codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

22 / 58 topics
21Error Handling22Panic!
Tutorials/Rust/Panic!
🦀Rust

Panic!

Updated 2026-05-15
10 min read

Panic!

Introduction

In Rust, error management is a critical aspect of writing robust and reliable software. One of the key concepts in Rust's error handling model is the use of panic!, which is used to handle unrecoverable errors. When a panic! occurs, it will terminate the program and unwind the stack, printing an error message to the console.

This tutorial will cover how to use panic! effectively, including when to use it and how to handle its effects in your code.

Concept

The panic! macro is used to indicate that something has gone wrong in a way that cannot be recovered from. It's typically used for programming errors or other critical issues that should not occur under normal circumstances.

When a panic! occurs, Rust will unwind the stack, dropping any variables that are no longer needed and running their destructors. This ensures that resources are cleaned up properly before the program exits.

Examples

Basic Usage

Let's start with a simple example to demonstrate how panic! works:

Rust
1fn main() {
2 panic!("Something went wrong!");
3}

When you run this code, Rust will terminate the program and print the error message:

Terminal
$ cargo run
Compiling panic_example v0.1.0 (/path/to/panic_example)
Finished dev [unoptimized + debuginfo] target(s) in 0.35s
Running `target/debug/panic_example`
thread 'main' panicked at 'Something went wrong!', src/main.rs:2:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target/debug/panic_example` (exit code: 101)

Using panic! in Functions

You can also use panic! within functions to handle errors that cannot be recovered from. For example:

Rust
1fn divide(a: i32, b: i32) -> i32 {
2 if b == 0 {
3 panic!("Division by zero!");
4 }
5 a / b
6}
7
8fn main() {
9 let result = divide(10, 0);
10 println!("Result: {}", result);
11}

In this example, the divide function checks if the divisor b is zero. If it is, it calls panic! to terminate the program with an error message.

Handling Panics

While panic! is used for unrecoverable errors, you can still handle its effects using Rust's unwind mechanism. For example, you can use a catch_unwind function from the std::panic module:

Rust
1use std::panic;
2
3fn main() {
4 let result = panic::catch_unwind(|| {
5 divide(10, 0);
6 });
7
8 match result {
9 Ok(_) => println!("Division succeeded!"),
10 Err(e) => println!("Caught a panic: {:?}", e),
11 }
12}
13
14fn divide(a: i32, b: i32) -> i32 {
15 if b == 0 {
16 panic!("Division by zero!");
17 }
18 a / b
19}

In this example, the catch_unwind function is used to catch the panic and handle it gracefully. The result of the closure is returned as an Ok variant if no panic occurs, or as an Err variant with the error information if a panic does occur.

What's Next?

Now that you understand how to use panic! for unrecoverable errors, the next step in your Rust journey could be exploring concurrency. Rust provides powerful tools for concurrent programming, including threads and message passing, which will allow you to write highly efficient and scalable applications.

Stay tuned for more tutorials on Rust's concurrency model!


PreviousError HandlingNext Concurrency

Recommended Gear

Error HandlingConcurrency