From 3ba5ffd976e9292a3a7a0168a106f9da936ef7a7 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 30 Jan 2019 14:52:10 -0500 Subject: [PATCH] Update enums script --- .idea/vcs.xml | 1 + .idea/workspace.xml | 46 ++++++++++++----------- enums/src/main.rs | 92 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 22 deletions(-) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index d2ab9f2..d362d9c 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index da0632f..8a3748f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,9 @@ - + + + - + - - + + - - + + - - - - - @@ -56,6 +53,7 @@ @@ -96,7 +94,7 @@ - + @@ -214,8 +212,8 @@ - - + + @@ -231,7 +229,7 @@ - + @@ -345,19 +343,23 @@ + + + + + + + - - + + - - + + - - - \ No newline at end of file diff --git a/enums/src/main.rs b/enums/src/main.rs index e7a11a9..fb6ee8d 100644 --- a/enums/src/main.rs +++ b/enums/src/main.rs @@ -1,3 +1,95 @@ +#[derive(Debug)] +enum UsState { + Alabama, + Alaska, + Arizona, + Arkansaw, + California, + Colorado, + Connecticut, + Delaware, + Florida, + Georgia, + Hawaii, + Idaho, + Illinois, + Indianan, + Iowa, + Kansas, + Kentucky, + Louisiana, + Maine, + Maryland, + Massachusetts, + Michigan, + Minnesota, + Mississippi, + Missouri, + Montana, + Nebraska, + Nevada, + New_Hampshire, + New_Jersey, + New_Mexico, + New_York, + North_Carolina, + North_Dakota, + Ohio, + Oklahoma, + Oregon, + Pennsylvania, + Rhode_Island, + South_Carolina, + South_Dakota, + Tennessee, + Texas, + Utah, + Vermont, + Virginia, + Washington, + West_Virginia, + Wisconsin, + Wyoming, +} + +enum Coin { + Penny, + Nickel, + Dime, + Quarter(UsState), +} + +fn value_in_cents(coin: Coin) -> u32 { + match coin { + Coin::Penny => { + println!("Lucky penny!"); + 1 + }, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter(state) => { + println!("State quarter from {:?}!", state); + 25 + }, + } +} + +fn count_non_quarters(count: &mut u32, coin: Coin) { + // If let is like matching on one condition of a match statement + if let Coin::Quarter(state) = coin { + println!("State quarter from {:?}!", state); + } else { + count += 1; + } +} + +fn plus_one(x: Option) -> Option { + match x { + None => None, // Match is exhaustive, must define the 'None' case + Some(i) => Some(i + 1), + } +} + fn main() { println!("Hello, world!"); }