Rust program to demonstrate the panic!() macro

Rust Example: Write a program to demonstrate the panic!() macro.
Submitted by Nidhi, on November 25, 2021

Problem Solution:

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 »




Comments and Discussions!

Load comments ↻





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