matching
This commit is contained in:
parent
25995eaf93
commit
1cbba18a4e
2 changed files with 57 additions and 0 deletions
8
17-matching/Cargo.toml
Normal file
8
17-matching/Cargo.toml
Normal file
|
@ -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]
|
49
17-matching/src/main.rs
Normal file
49
17-matching/src/main.rs
Normal file
|
@ -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!"),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue