How it works...

We have a function named compare_stmt that accepts an str variable as an argument and assigns it to a variable named input in its scope. It later checks whether the string value is Another Book. If it is, it calls the panic! function; otherwise, it prints the value that was passed. We passed two values from the main function: Rust CookBook and `Another Book`.

When we run the preceding program, the first input will fail the if condition and will not invoke panic, so we get the print statement working. However, for the second input, which satisfies the if condition, panic is invoked and it returns thread 'main' panicked at 'Rust Cookbook is not selected!!!!', sample_panic.rs:12 and exits the program.

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

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