summaryrefslogtreecommitdiff
path: root/matching/src
diff options
context:
space:
mode:
Diffstat (limited to 'matching/src')
-rw-r--r--matching/src/main.rs51
1 files changed, 51 insertions, 0 deletions
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"),
+ _ => (),
+ }
+}