Rust program to demonstrate the panic!() macro

Rust Example: Write a program to demonstrate the panic!() macro.
Last Updated : November 25, 2021

Problem Statement

In this program, we will demonstrate panic!() macro. The panic!() macro terminates the program immediately and provides feedback.

Program/Source Code

The source code to demonstrate the panic!() macro is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate 
// the panic!() macro

fn main() {
   panic!("Error Occurred");
   println!("End of the program"); 
}

Output

thread 'main' panicked at 'Error Occurred', main.rs:5:4
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Explanation

In the main() function, we called panic!() macro with the message "Error Occurred". That's why the program gets terminated immediately by calling println!() macro. The panic!() macro terminates the program immediately and provides feedback.

Rust Miscellaneous Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.