summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs')
-rw-r--r--rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs b/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs
new file mode 100644
index 0000000..f419cc9
--- /dev/null
+++ b/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs
@@ -0,0 +1,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),
+ }
+}