We declared two enum types, namely Food and Day, where we had these elements: CordonBleu, Steak, and Sushi and Monday, Tuesday, and Wednesday.
Now let's see how our function units react to different input:
- have_ingredients: This function takes in the enum input Food and returns Option<Food>. It has a case in its match statement indicating whether Food has the value Sushi in that it returns None and for all other values, it returns the same Food value.
- have_recipe: This function takes in the enum input Food and returns Option<Food>. It has a case in its match statement indicating whether Food has the value CordonBleu in that it returns None; for all other values, it returns the same Food value.
- cookable: This function takes in the enum input Food and returns Option<Food>. Here, we use the and_then combinator to check the have_ingredients and have_recipe functions in order to confirm that the Food type will pass these cases.
- eat: This function takes the enum input Food and Day and sends the Food value to the cookable function. Here we have a match statement that prints the day and Food type in the case of Some(Food) from the cookable function.
We observe that for cookable to return a value, we need both the functions have_ingredients and have_ recipe to return Some(Food), which happens only in the case of Steak. In the main function, we called the eat function with all the values of Food and Day.