rust_programming_book 0.1.1

Programming works from THE RUST PROGRAMMING LANGUAGE
Documentation
#[allow(dead_code)]
pub enum Coin {
    Penny(u8),
    Cent(u8),
    Ten(u8),
    TwentyFive(u8),
}

/// Option<T> is used when we've to check whether there is value or not
/// The Option<T> enum has two variants:
///None, to indicate failure or lack of value, and
///Some(value), a tuple struct that wraps a value with type T.
#[allow(dead_code, unused_variables)]
fn is_valid(coin: Option<&Coin>) {
    match coin {
        Some(_coin) => println!("valid money"),
        None => println!("invalid money"),
    }
}

#[allow(dead_code)]
impl Coin {
    fn check_money(&self) {
        match self {
            Self::Penny(value) => println!("It's penny, total worth: {}", value),
            Self::Ten(value) => println!("It's ten, total worth: {}", value),
            Self::Cent(value) => println!("It's cent, total worth: {}", value),
            Self::TwentyFive(value) => println!("It's twenty five, total worth: {}", value),
        }
    }
}
#[allow(dead_code, unused_variables)]
pub fn control_flow() {
    let money1 = Coin::Penny(20);
    let money2 = Coin::TwentyFive(25);
    is_valid(Some(&money1));
    is_valid(Some(&money2));

    money1.check_money();
    money2.check_money();
}
/**

 * concise control flow can be done with if let statement. It works
 * in a similar manner like match does. It just check one (in most case) and
 * ignores other thus it reduces boiler plate code.
 */
#[allow(dead_code, unused_variables)]
pub fn concise_control_flow() {
    let money = Coin::Penny(20);
    let money1 = Coin::Cent(100);

    // here is money1 matches the Coin::Penny(penny:u8) then perform some work inside {}, it is
    // less verbose and works like match
    if let Coin::Penny(penny) = money {
        println!("Money is penny and it's worth is : {}", penny)
    } else {
        println!("Money is not penny!")
    }
}