diff options
author | Jordan Gong <jordan.gong@protonmail.com> | 2020-08-17 16:04:13 +0800 |
---|---|---|
committer | Jordan Gong <jordan.gong@protonmail.com> | 2020-08-17 16:04:13 +0800 |
commit | b80354d75b58858f9c90a9c52e2bceb1ee013cdb (patch) | |
tree | a9c05146f7481e7e659576c7916fe4c7ba4aef9a /matching/src/main.rs | |
parent | c2f2aa6c14e3232dd015d25c923e6f32c1da0e92 (diff) |
Match with `if let`
Diffstat (limited to 'matching/src/main.rs')
-rw-r--r-- | matching/src/main.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/matching/src/main.rs b/matching/src/main.rs index 55c41d0..1da48a0 100644 --- a/matching/src/main.rs +++ b/matching/src/main.rs @@ -11,23 +11,31 @@ enum Coin { Quarter(UsState), } -fn value_in_cents(coin: Coin) -> u8 { +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)); + let coin = Coin::Quarter(UsState::Alaska); + value_in_cents(&coin); + + let mut count = 0; + if let Coin::Quarter(state) = coin { + println!("State quarter from: {:?}!", state); + } else { + count += 1; + } fn plus_one(x: Option<i32>) -> Option<i32> { match x { @@ -48,4 +56,9 @@ fn main() { 7 => println!("seven"), _ => (), } + + let some_u8_value = Some(0u8); + if let Some(3) = some_u8_value { + println!("three"); + } } |