if_let
This commit is contained in:
parent
1cbba18a4e
commit
f5920a69ae
2 changed files with 50 additions and 0 deletions
8
18-if_let/Cargo.toml
Normal file
8
18-if_let/Cargo.toml
Normal file
|
@ -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]
|
42
18-if_let/src/main.rs
Normal file
42
18-if_let/src/main.rs
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue