rust enums and pattern matching

Defining an enum

enum IpAddrKind {
    V4,
    V6,
}
 
fn main() {
    let four = IpAddrKind::V4;
    let six = IpAddrKind::V6;
 
    route(IpAddrKind::V4);
    route(IpAddrKind::V6);
}
 
fn route(ip_kind: IpAddrKind) {}
fn main() {
    // different data structure for each enum value
    enum IpAddr {
        V4(u8, u8, u8, u8),
        V6(String),
    }
 
    let home = IpAddr::V4(127, 0, 0, 1);
 
    let loopback = IpAddr::V6(String::from("::1"));
}
 
#![allow(unused)]
fn main() {
struct Ipv4Addr {
    // --snip--
}
 
struct Ipv6Addr {
    // --snip--
}
 
// enum with struct as field
enum IpAddr {
    V4(Ipv4Addr),
    V6(Ipv6Addr),
}
}
// powerful enum usage
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}
 
fn main() {}

Match control flow operator

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter,
}
 
fn value_in_cents(coin: Coin) -> u8 {
    match coin {
        // Curly brackets typically aren’t used if the match arm code is short
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter => 25,
    }
}
 
fn main() {}
#[derive(Debug)]
enum UsState {
    Alabama,
    Alaska,
    // --snip--
}
 
enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter(UsState),
}
 
fn value_in_cents(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        // patterns that bind to values
        Coin::Quarter(state) => {
            println!("State quarter from {:?}!", state);
            25
        }
    }
}
 
fn main() {
    value_in_cents(Coin::Quarter(UsState::Alaska));
}

Matches are exhaustive, Rust knows how if every possible case are not cover and even knows which pattern is forgotten.

fn main() {
    let dice_roll = 9;
    match dice_roll {
        3 => add_fancy_hat(),
        7 => remove_fancy_hat(),
        // catch-all pattern with _ placeholder
        _ => reroll(),
    }
 
    fn add_fancy_hat() {}
    fn remove_fancy_hat() {}
    fn reroll() {}
}

Concise control flow

#[derive(Debug)]
enum UsState {
    Alabama,
    Alaska,
    // --snip--
}
 
enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter(UsState),
}
 
fn main() {
    let coin = Coin::Penny;
    let mut count = 0;
    if let Coin::Quarter(state) = coin {
        println!("State quarter from {:?}!", state);
    } else {
        count += 1;
    }
}