1. ```rust use std::num::ParseIntError; fn multiply(n1_str: &str, n2_str: &str) -> Result { let n1 = n1_str.parse::(); let n2 = n2_str.parse::(); Ok(n1.unwrap() * n2.unwrap()) } fn main() { let result = multiply("10", "2"); assert_eq!(result, Ok(20)); let result = multiply("4", "2"); assert_eq!(result.unwrap(), 8); println!("Success!") } ``` 2. ```rust use std::num::ParseIntError; // IMPLEMENT multiply with ? // DON'T use unwrap here fn multiply(n1_str: &str, n2_str: &str) -> Result { let n1 = n1_str.parse::()?; let n2 = n2_str.parse::()?; Ok(n1 * n2) } fn main() { assert_eq!(multiply("3", "4").unwrap(), 12); println!("Success!") } ``` 3. ```rust use std::fs::File; use std::io::{self, Read}; fn read_file1() -> Result { let f = File::open("hello.txt"); let mut f = match f { Ok(file) => file, Err(e) => return Err(e), }; let mut s = String::new(); match f.read_to_string(&mut s) { Ok(_) => Ok(s), Err(e) => Err(e), } } fn read_file2() -> Result { let mut s = String::new(); File::open("hello.txt")?.read_to_string(&mut s)?; Ok(s) } fn main() { assert_eq!(read_file1().unwrap_err().to_string(), read_file2().unwrap_err().to_string()); println!("Success!") } ``` 4. ```rust use std::num::ParseIntError; fn add_two(n_str: &str) -> Result { n_str.parse::().map(|num| num +2) } fn main() { assert_eq!(add_two("4").unwrap(), 6); println!("Success!") } ``` ```rust use std::num::ParseIntError; fn add_two(n_str: &str) -> Result { n_str.parse::().and_then(|num| Ok(num +2)) } fn main() { assert_eq!(add_two("4").unwrap(), 6); println!("Success!") } ``` 5. ```rust use std::num::ParseIntError; // With the return type rewritten, we use pattern matching without `unwrap()`. // But it's so Verbose.. fn multiply(n1_str: &str, n2_str: &str) -> Result { match n1_str.parse::() { Ok(n1) => { match n2_str.parse::() { Ok(n2) => { Ok(n1 * n2) }, Err(e) => Err(e), } }, Err(e) => Err(e), } } // Rewriting `multiply` to make it succinct // You MUST USING `and_then` and `map` here fn multiply1(n1_str: &str, n2_str: &str) -> Result { // IMPLEMENT... n1_str.parse::().and_then(|n1| { n2_str.parse::().map(|n2| n1 * n2) }) } fn print(result: Result) { match result { Ok(n) => println!("n is {}", n), Err(e) => println!("Error: {}", e), } } fn main() { // This still presents a reasonable answer. let twenty = multiply1("10", "2"); print(twenty); // The following now provides a much more helpful error message. let tt = multiply("t", "2"); print(tt); println!("Success!") } ``` 6. ```rust use std::num::ParseIntError; // Define a generic alias for a `Result` with the error type `ParseIntError`. type Res = Result; // Use the above alias to refer to our specific `Result` type. fn multiply(first_number_str: &str, second_number_str: &str) -> Res { first_number_str.parse::().and_then(|first_number| { second_number_str.parse::().map(|second_number| first_number * second_number) }) } // Here, the alias again allows us to save some space. fn print(result: Res) { match result { Ok(n) => println!("n is {}", n), Err(e) => println!("Error: {}", e), } } fn main() { print(multiply("10", "2")); print(multiply("t", "2")); } ```