forked from inancgumus/learnrust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
108 lines (98 loc) · 3.23 KB
/
main.rs
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
enum UsState {
Alabama,
Alaska,
// ...
}
// each Coin variant holds a u64 value.
enum Coin {
Penny(u64),
Nickel(u64),
Dime(u64),
// this one also holds two values: a u64, and a UsState.
Quarter(u64, UsState),
}
fn main() {
println!("10 pennys is {} cent(s).", Coin::Penny(10).cents());
println!("10 nickels is {} cent(s).", Coin::Nickel(10).cents());
println!("10 dimes is {} cent(s).", Coin::Dime(10).cents());
println!(
"10 Alabama Quarters is {} cent(s).",
Coin::Quarter(10, UsState::Alabama).cents()
);
println!(
"10 Alaska Quarters is {} cent(s).",
Coin::Quarter(10, UsState::Alaska).cents()
);
// ----------------------------------------------------------------------
// match is exhaustive.
// -> this means that you need to exercise all the variants of a value.
let n = 3u8;
match n {
0 => println!("zero"),
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
// however, _ will match any value.
// so you can exhaust a match this way.
// uncomment this and see what happens.
_ => (), // () is the unit value, literally empty.
}
// ----------------------------------------------------------------------
// In Rust, there isn't a `null` value.
//
// However, there is an Option enum which can either be:
//
// Some
// None
//
// Some: Option value stores data.
// None: Option value stores nothing.
//
// This way, Rust prevents the Billion-Dollar Null value mistake.
//
// You always need to use match to look inside the option value,
// and find out if it has a value inside (Some), or nothing (None).
//
let mut n = Some(6);
match n {
Some(6) => println!("six"),
_ => (),
}
// However, this can be cumbersome.
// Especially, if you only want to print if `n` is 6, as above.
//
// There is a better way:
if let Some(6) = n {
println!("six");
}
// `if let` is syntactic sugar for a match that runs code when the value
// matches one pattern and then ignores all other values.
// using an else clause to match other values than 6 is also possible.
n = Some(7);
if let Some(6) = n {
println!("six");
} else {
println!("not six, it's {}.", n.unwrap_or_default());
}
}
impl Coin {
// this method can be used from an instance of the Coin enum.
fn cents(&self) -> u64 {
// match an arm depending on the variant of the Coin enum:
// Penny, Nickel, Dime, and Quarter.
// `amount` is being populated from a variant's data.
match self {
Coin::Penny(amount) => amount * 1, // this arm returns 1
Coin::Nickel(amount) => amount * 5, // this arm returns 5
Coin::Dime(amount) => amount * 10, // this arm returns 10
Coin::Quarter(amount, state) => {
let c = match state {
UsState::Alabama => 15, // this arm returns 15
UsState::Alaska => 25, // this arm return 10
};
amount * c
}
}
}
}