summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way
diff options
context:
space:
mode:
authorgarhve <git@garhve.com>2022-12-27 15:59:44 +0800
committergarhve <git@garhve.com>2022-12-27 15:59:44 +0800
commit8a8302fa5d94425c01b358de57c071d32af6b7ed (patch)
treefa91d38a9444cf123fde3faeec07eabe3c87620f /rust/theBook/chapter-6-enums-and-pattern-matching/enum-way
parent3172b239863e1151d85197fdb4ea3e42d421256e (diff)
finish chapter 6
Diffstat (limited to 'rust/theBook/chapter-6-enums-and-pattern-matching/enum-way')
-rw-r--r--rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/Cargo.toml8
-rw-r--r--rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs40
2 files changed, 48 insertions, 0 deletions
diff --git a/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/Cargo.toml b/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/Cargo.toml
new file mode 100644
index 0000000..4e103bd
--- /dev/null
+++ b/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "enum-way"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
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),
+ }
+}