rust_learning/enum_match/
match_def.rs1
2#[derive(Debug)] enum UsState {
4 Alabama,
5 Alaska,
6 }
8
9enum Coin {
10 Penny,
11 Nickel,
12 Dime,
13 Quarter(UsState),
14}
15
16fn value_in_cents(coin: Coin) -> u8 {
17 match coin {
25 Coin::Penny => 1,
26 Coin::Nickel => 5,
27 Coin::Dime => 10,
28 Coin::Quarter(state) => {
29 println!("State quarter from {:?}!", state);
30 25
31 }
32 }
33}
34
35pub fn match_def() {
37 let some_u8_value = Some(0);
40 let a = 15;
41
42 let i = value_in_cents(Coin::Penny);
43 println!("i is {}", i);
44
45 let i = value_in_cents(Coin::Quarter(UsState::Alabama));
46 println!("i is {}", i)
47}
48
49pub fn option_match() {
51 let five = Some(5);
52 let six = plus_one(five);
53 println!("six is {:?}", six);
54 let none = plus_one(None);
55 println!("none is {:?}", none)
56}
57
58fn plus_one(x: Option<i32>) -> Option<i32> {
59 match x {
60 None => None,
62 Some(i) => Some(i + 1)
63 }
64}
65
66
67pub fn match_other() {
69 let x = 8;
70 match x {
71 1 | 2 => println!("one or two"),
72 3 => println!("three"),
73 _other => println!("anything"),
75 }
76
77 match x {
79 1..=5 => println!("one through five"),
80 _ => println!("something else"),
81 }
82
83 match x {
84 1..=5 => println!("one through five"),
85 _ => (),
86 }
87}