From f5920a69ae78c1c17a00c9ffc51e9c4583e64d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Sat, 9 Sep 2023 02:14:45 +0300 Subject: [PATCH] if_let --- 18-if_let/Cargo.toml | 8 ++++++++ 18-if_let/src/main.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 18-if_let/Cargo.toml create mode 100644 18-if_let/src/main.rs 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); + } + } + }