How to do it...

Follow the ensuing steps to get through this recipe:

  1. Create a file named sample_panic.rs and open it in your text editor.
  2. Write the code header with the relevant information:
        //-- #########################
//-- Task: Implementing panic
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 26 March 17
//-- #########################
  1. 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);
}
  1. 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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.128