summaryrefslogtreecommitdiff
path: root/matching
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2020-08-17 15:49:53 +0800
committerJordan Gong <jordan.gong@protonmail.com>2020-08-17 15:49:53 +0800
commitc2f2aa6c14e3232dd015d25c923e6f32c1da0e92 (patch)
treebd0a0ed6ebda9a154e101a94a3a34cd28056223d /matching
parente15d77f54a26988ea1715cf4be6ec3f858772da4 (diff)
Match pattens
Diffstat (limited to 'matching')
-rw-r--r--matching/.gitignore18
-rw-r--r--matching/Cargo.toml9
-rw-r--r--matching/src/main.rs51
3 files changed, 78 insertions, 0 deletions
diff --git a/matching/.gitignore b/matching/.gitignore
new file mode 100644
index 0000000..e629269
--- /dev/null
+++ b/matching/.gitignore
@@ -0,0 +1,18 @@
+
+# Created by https://www.toptal.com/developers/gitignore/api/rust
+# Edit at https://www.toptal.com/developers/gitignore?templates=rust
+
+### Rust ###
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
+Cargo.lock
+
+# These are backup files generated by rustfmt
+**/*.rs.bk
+
+# End of https://www.toptal.com/developers/gitignore/api/rust
+
diff --git a/matching/Cargo.toml b/matching/Cargo.toml
new file mode 100644
index 0000000..7b5c514
--- /dev/null
+++ b/matching/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "matching"
+version = "0.1.0"
+authors = ["Jordan Gong <jordan.gong@protonmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/matching/src/main.rs b/matching/src/main.rs
new file mode 100644
index 0000000..55c41d0
--- /dev/null
+++ b/matching/src/main.rs
@@ -0,0 +1,51 @@
+#[derive(Debug)]
+enum UsState {
+ Alabama,
+ Alaska,
+}
+
+enum Coin {
+ Penny,
+ Nickle,
+ Dime,
+ Quarter(UsState),
+}
+
+fn value_in_cents(coin: Coin) -> u8 {
+ match coin {
+ Coin::Penny => {
+ println!("Lucky penny!");
+ 1
+ },
+ Coin::Nickle => 5,
+ Coin::Dime => 10,
+ Coin::Quarter(state) => {
+ println!("State quarter from: {:?}!", state);
+ 25
+ },
+ }
+}
+
+fn main() {
+ value_in_cents(Coin::Quarter(UsState::Alaska));
+
+ fn plus_one(x: Option<i32>) -> Option<i32> {
+ match x {
+ None => None,
+ Some(x) => Some(x + 1),
+ }
+ }
+
+ let five = Some(5);
+ let six = plus_one(Some(5));
+ let none = plus_one(None);
+
+ let some_u8_value = 0u8;
+ match some_u8_value {
+ 1 => println!("one"),
+ 3 => println!("three"),
+ 5 => println!("five"),
+ 7 => println!("seven"),
+ _ => (),
+ }
+}