diff --git a/17-matching/Cargo.toml b/17-matching/Cargo.toml new file mode 100644 index 0000000..81bbaa1 --- /dev/null +++ b/17-matching/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "matching" +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/17-matching/src/main.rs b/17-matching/src/main.rs new file mode 100644 index 0000000..c3d7449 --- /dev/null +++ b/17-matching/src/main.rs @@ -0,0 +1,49 @@ +#[derive(Debug)] +enum Fruits + { + Apple, + Banana, + Strawberry + } +enum Cake + { + Chocolate, + Caramel, + Fruit(Fruits), + Tahini, + Butter, + Vanilin, + } +fn main() + { + println!("Hello, world!"); + let cake1 = Cake::Chocolate; + let cake2 = Cake::Caramel; + let cake3 = Cake::Fruit(Fruits::Apple); + let cake4 = Cake::Fruit(Fruits::Banana); + let cake5 = Cake::Fruit(Fruits::Strawberry); + let cake6 = Cake::Tahini; + let cake7 = Cake::Butter; + let cake8 = Cake::Vanilin; + + let cakes = [cake1,cake2,cake3,cake4,cake5,cake6,cake7,cake8]; + for i in cakes + { + // our dear match always warn us to cover all posibilities + // they are like switch case but better I think + // they can be useful with enums + match i + { + Cake::Chocolate => println!("Chocolate"), + Cake::Caramel => println!("Caramel"), + Cake::Fruit(name) => println!("Fruit {:#?}", name), + Cake::Tahini => println!("Tahini"), + // this one is wildcard + // but when we don't want to use variable + // we can use it to cover all posibilities + // that's why it's the last one + _ => println!("Others!"), + } + } + + }