Follow the ensuing steps to get through this recipe:
- Create a file named sample_panic.rs and open it in your text editor.
- Write the code header with the relevant information:
//-- #########################
//-- Task: Implementing panic
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 26 March 17
//-- #########################
- Create the compare_stmt function; it accepts a string input:
// function which checks if the strings are same or not
fn compare_stmt(stmt: &str) {
// Check if the statements are same or not
if stmt == "Another book" {
panic!("Rust Cookbook is not selected!!!!");
}
println!("Statements is {}!!!!!", stmt);
}
- Define the main function; it calls the compare_stmt function with different input:
// Execution starts here
fn main() {
compare_stmt("Rust Cookbook");
compare_stmt("Another book");
}
You will get the following output upon successful execution of the code: