diff options
Diffstat (limited to 'rust/theBook/chapter-6-enums-and-pattern-matching')
-rw-r--r-- | rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/Cargo.toml | 8 | ||||
-rw-r--r-- | rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs | 40 |
2 files changed, 48 insertions, 0 deletions
diff --git a/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/Cargo.toml b/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/Cargo.toml new file mode 100644 index 0000000..4e103bd --- /dev/null +++ b/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "enum-way" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs b/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs new file mode 100644 index 0000000..f419cc9 --- /dev/null +++ b/rust/theBook/chapter-6-enums-and-pattern-matching/enum-way/src/main.rs @@ -0,0 +1,40 @@ +#[derive(Debug)] + +enum UpState { + Alabama, + Alaska, +} + +enum Coin { + Penny, + Nickel, + Dime, + Quarter(UpState), +} + +fn main() { + let state = UpState::Alaska; + let coin = Coin::Quarter(state); + println!("the quater is {}",value_in_cents(coin)); + println!("x = {:?}", plus_one(None)); +} + +fn value_in_cents(coin: Coin) -> u8 { + match coin { + Coin::Penny => 1, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter(state) => { + println!("state quarter from {:?}",state); + 25 + } + } +} + + +fn plus_one(x: Option<i32>) -> Option<i32> { + match x { + None => None, + Some(i) => Some(i+1), + } +} |