Last active
May 3, 2022 16:05
-
-
Save soyart/c02a56723931d172096757932e91dcb3 to your computer and use it in GitHub Desktop.
My practice code for https://doc.rust-lang.org/book/ch06-00-enums.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![allow(dead_code)] | |
| use std::fmt; | |
| enum OrderStatus { | |
| Null, | |
| Created, | |
| Cancelled, | |
| Successful, | |
| } | |
| struct Order { | |
| id: u64, | |
| status: OrderStatus, | |
| } | |
| enum Coin { | |
| Penny, | |
| Nickle, | |
| Dime, | |
| Quater, | |
| } | |
| // Impl Debug for enum Coin | |
| impl fmt::Debug for Coin { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| match *self { | |
| Coin::Penny => write!(f, "penny"), | |
| Coin::Nickle => write!(f, "nickle"), | |
| Coin::Dime => write!(f, "dime"), | |
| Coin::Quater => write!(f, "quater"), | |
| } | |
| } | |
| } | |
| fn main() { | |
| let _order = Order{id: 0, status: OrderStatus::Cancelled}; | |
| let my_int = 70; | |
| let some_int = Some(69); | |
| let no_int: Option<i32> = None; | |
| // println!("{}", my_int + some_int);// invalid type | |
| // println!("{} + {:?}.unwrap() = {}", my_int, no_int, my_int + no_int.unwrap()); // will panic when unwrapping | |
| println!("{} + {:?}.unwrap() = {}", my_int, some_int, my_int + some_int.unwrap()); | |
| println!("{} + {:?}.unwrap_or(0) = {}", my_int, no_int, my_int + no_int.unwrap_or(0)); | |
| let mut coin = Coin::Penny; | |
| println!("value of coin {:?} = {}", coin, coin_val(&coin)); | |
| coin = Coin::Nickle; | |
| println!("value of coin {:?} = {}", coin, coin_val(&coin)); | |
| println!("plus_ten({}) = {:?}", some_int.unwrap(), plus_ten(some_int)); | |
| // println!("plus_ten({}) = {:?}", no_int.unwrap(), plus_ten(no_int)); // will panic when unwrapping | |
| println!("plus_ten(None) = {:?}", plus_ten(no_int)); | |
| // Without if-let | |
| let some_int = Some(69); | |
| match some_int { | |
| None => { | |
| println!("found None!") | |
| } | |
| Some(69) => { | |
| println!("ooh 69!") | |
| } | |
| _ => { | |
| println!("no 69 no life") | |
| } | |
| } | |
| // With if-let (not exhaustive) | |
| // checks if some_int is Some(69) | |
| if let Some(69) = some_int { | |
| println!("69 is {}", some_int.unwrap()) | |
| } else { | |
| println!("sad not 69"); | |
| } | |
| // Alternative if-let | |
| let _six_nine = some_int.unwrap(); | |
| if let Some(_six_nine) = some_int { | |
| println!("some_int is {:?}", some_int) | |
| } | |
| } | |
| fn coin_val(c: &Coin) -> u8 { | |
| match c { | |
| Coin::Penny => 1, | |
| Coin::Nickle => 5, | |
| Coin::Dime => 10, | |
| Coin::Quater => 25, | |
| } | |
| } | |
| fn plus_ten(x: Option<i32>) -> Option<i32> { | |
| match x { | |
| None => None, | |
| Some(i) => Some(i+10), // wrap result in Some, because this func returns Option<i32> | |
| } | |
| } | |
| fn return_int() -> i32 { | |
| 96 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment