// ./src/error/result/result_map.md use std::num::ParseIntError; // With the return type rewritten, we use pattern matching without `unwrap()`. fn multiply(first_number_str: &str, second_number_str: &str) -> Result { match first_number_str.parse::() { Ok(first_number) => { match second_number_str.parse::() { Ok(second_number) => { Ok(first_number * second_number) }, Err(e) => Err(e), } }, Err(e) => Err(e), } } fn print(result: Result) { match result { Ok(n) => println!("n is {}", n), Err(e) => println!("Error: {}", e), } } fn part0() { // This still presents a reasonable answer. let twenty = multiply("10", "2"); print(twenty); // The following now provides a much more helpful error message. let tt = multiply("t", "2"); print(tt); } // As with `Option`, we can use combinators such as `map()`. // This function is otherwise identical to the one above and reads: // Modify n if the value is valid, otherwise pass on the error. fn multiply2(first_number_str: &str, second_number_str: &str) -> Result { first_number_str.parse::().and_then(|first_number| { second_number_str.parse::().map(|second_number| first_number * second_number) }) } fn print2(result: Result) { match result { Ok(n) => println!("n is {}", n), Err(e) => println!("Error: {}", e), } } fn part1() { // This still presents a reasonable answer. let twenty = multiply2("10", "2"); print(twenty); // The following now provides a much more helpful error message. let tt = multiply2("t", "2"); print(tt); } pub fn main() { part0(); part1(); }