# Match Control Flow operator As seen previously `match` is used like a switch, it compares a value and has an outcope for each possible enum option: ```rust enum Coin { Penny, Nickel, Dime, Quarter, } //--- fn value_in_cents(coin: Coin) -> u8 { match coin { Coin::Penny => 1, Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, } } ``` The match condition returns the type of enum value it is, so that then it executes the one it is intended for. This is also the way to get the associated data to an enum value ```rust enum Coin { Penny, Nickel, Dime, Quarter(u16),// Year of minting } //... match coin { Coin::Quarter(year) => { // Gets the associated vars in same order println!("Quarter made in {}", year); year // return value is the year and now match will return the year } } ``` ## Match with Option We can operate on the value that Option has if we know the type, Ex: ```rust fn plus_one(x: Option) -> Option { match x { None => None, Some(i) => Some(i + 1), } } let five = Some(5); let six = plus_one(five); let none = plus_one(None); ``` But in `match` we have to make sure we take into account all cases, the following would not compile: ```rust fn plus_one(x: Option) -> Option { match x { Some(i) => Some(i + 1), } } ``` It is missing hte `None` arm of the option, so it throws an error. ## Catch-All Patterns -> _Placeholder ```rust let dice_roll = 9; // Other / whatever name not part of the enum match dice_roll { 3 => add_fancy_hat(), 7 => remove_fancy_hat(), other => move_player(other), } // _ -> Value is not used match dice_roll { 3 => add_fancy_hat(), 7 => remove_fancy_hat(), _ => (), // Not executing code, just returning a unit value } ``` Basically, we can add a default case which would make use of the enum value, or a `_` that would not use it. # Concise flow control - If Let ```rust let config_max = Some(3u8); // Some of type u8 with value 3 // Default match with _ to just take care of Some(valid value) case match config_max { Some(max) => println!("Valid max {}", max), _ =>(), }; // If Let flow? if let Some(var) = config_max { println!("Configured max {}", var); } ``` The if let floiw is more concise, but lets destructure it. `if (let Some(var) = config_max) /*then*/ { /* Execute some code with *var* */}` The Condition `(let Some(var) = config_max)` will bind temporarily the vlaue of config_max to a nonexistent `Some(T)` variable, and the vlaue of `config_max(T)` will be set onto `var`. But, if config_max were not to be able to be bound to `Some(T)` then the conditional would fail and not execute! Then we can use `else`/`else if` to continue the statements.