Match Control flow

Match Control flow

03 June, 2020

match is a control flow operator. It works similar to a coin sorting machine, i.e. it would start matching patterns(a.k.a arms) from top to bottom until it finds a matching pattern. Example:

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter
}

fn match_coin(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter => 25,
    }
}

fn main() {
    let c = match_coin(Coin::Penny);
    println!("{}", c);
}

[Skipped this chapter. Gotta finish it later on]

abdus s. azad
About the Author
Abdus is a Software Engineer working at Level. In his free time, he likes to play with Linux-based machines. Contact him at abdus@abdus.net.