summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs
blob: f419cc970c761045c8b3c287449047e2dfa29905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#[derive(Debug)]

enum UpState {
    Alabama,
    Alaska,
}

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter(UpState),
}

fn main() {
    let state = UpState::Alaska;
    let coin = Coin::Quarter(state);
    println!("the quater is {}",value_in_cents(coin));
    println!("x = {:?}", plus_one(None));
}

fn value_in_cents(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter(state) => {
            println!("state quarter from {:?}",state);
            25
        }
    }
}


fn plus_one(x: Option<i32>) -> Option<i32> {
    match x {
        None => None,
        Some(i) => Some(i+1),
    }
}