How to Use the Option and Result Combinators (and_then, map, etc.)

Use map to transform values inside Option/Result and and_then to chain fallible operations in Rust.

Use map to transform the inner value of an Option or Result without unwrapping, and and_then to chain operations that might fail.

let five = Some(5);
let six = five.map(|x| x + 1);

let result: Result<i32, &str> = Ok(5);
let doubled = result.and_then(|x| if x > 0 { Ok(x * 2) } else { Err("negative") });