How to do it...

Follow the ensuing steps to get through this recipe:

  1. Create a file named sample_option.rs and open it in your text editor.
  2. Write the code header with the relevant information:

 

        //-- #########################
//-- Task: Implementing Option
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 26 March 17
//-- #########################
  1. Create the compare_stmt_match function; it accepts the input string of the Option<&str> type:

 

        // All arguments are handled explicitly using `match`.
fn compare_stmt_match(input: Option<&str>) {
// Specify a course of action for each case.
match input {
Some("Rust CookBook") => println!("Rust CookBook
was selected"),
Some(inner) => println!("Rust CookBook not
selected"),
None => println!("No input provided"),
}
}
  1. Similarly, create the compare_stmt_unwrap function; it also accepts the input string of the Option<&str> type:

 

        // All arguments are handled implicitly using `unwrap`.
fn compare_stmt_unwrap(input: Option<&str>) {
// `unwrap` returns a `panic` when it receives a
`None` value
let inside_val = input.unwrap();
if inside_val == "Another Book" { panic!("Rust
CookBook is not selected"); }

println!("I love {}s!!!!!", inside_val);
}
  1. Define the main function; it calls the two functions with different input:
        // main execution starts here
fn main() {
let Desired_Book = Some("Rust CookBook");
let Another_Book = Some("Another Book");
let Empty_value = None;

compare_stmt_match(Desired_Book);
compare_stmt_match(Another_Book);
compare_stmt_match(Empty_value);

println!("*********************");

let Rand_Book = Some("Random Book");
let No_val = None;

compare_stmt_unwrap(Rand_Book);
compare_stmt_unwrap(No_val);
}

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.141.200.3