diff --git a/18-if_let/Cargo.toml b/18-if_let/Cargo.toml new file mode 100644 index 0000000..c652367 --- /dev/null +++ b/18-if_let/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "if_let" +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/18-if_let/src/main.rs b/18-if_let/src/main.rs new file mode 100644 index 0000000..ca2fc77 --- /dev/null +++ b/18-if_let/src/main.rs @@ -0,0 +1,42 @@ +enum Vehicle + { + Car, + Bus, + Truck, + Bicycle, + Scooter, + } +fn main() + { + println!("Hello, world!"); + + let vehicle1 = Vehicle::Car; + let vehicle2 = Vehicle::Bus; + let vehicle3 = Vehicle::Truck; + + let vehicles = [vehicle1,vehicle2,vehicle3]; + + for i in vehicles + { + // it's like match actually + // but less boilerplate + // this does not force us to cover all posibilities + // but it can, if you want + if let i == Vehicle::Car + { + println!("{} is Car", i); + } + if let i == Vehicle::Bus + { + println!("{} is Bus", i); + } + if let i == Vehicle::Truck + { + println!("{} is Truck", i); + } + else + { + println!("{} is Another Thing", i); + } + } + }