//-- ######################### //-- Task: Implementing and_then //-- Author: Vigneshwer.D //-- Version: 1.0.0 //-- Date: 26 March 17 //-- ######################### #![allow(dead_code)] #[derive(Debug)] enum Food { CordonBleu, Steak, Sushi } #[derive(Debug)] enum Day { Monday, Tuesday, Wednesday } // We don't have the ingredients to make Sushi. fn have_ingredients(food: Food) -> Option { match food { Food::Sushi => None, _ => Some(food), } } // We have the recipe for everything except Cordon Bleu. fn have_recipe(food: Food) -> Option { match food { Food::CordonBleu => None, _ => Some(food), } } // This can conveniently be rewritten more compactly with `and_then()`: fn cookable(food: Food) -> Option { have_ingredients(food).and_then(have_recipe) } fn eat(food: Food, day: Day) { match cookable(food) { Some(food) => println!("Yay! On {:?} we get to eat {:?}.", day, food), None => println!("Oh no. We don't get to eat on {:?}?", day), } } fn main() { let (cordon_bleu, steak, sushi) = (Food::CordonBleu, Food::Steak, Food::Sushi); eat(cordon_bleu, Day::Monday); eat(steak, Day::Tuesday); eat(sushi, Day::Wednesday); }