-
Notifications
You must be signed in to change notification settings - Fork 2
/
pattern_matching.rs
147 lines (132 loc) · 4.31 KB
/
pattern_matching.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::fmt::Error;
use rand::Rng;
/**
Very powerful pattern matching functionality like in Scala or Haskell, provide the functionality
to match types, unwrap values from container types like [Option], [Result].
Also it is possible do some predicate functions in each case to match the value under some conditions.
*/
pub fn run() {
primitive_types();
enum_types();
option_type();
result_type();
predicate_conditions();
struct_type();
tuple_type();
}
/**
Pattern matching in rust it can match any enum type, and knowing the static limits of enum
offer in the match all options.
*/
fn enum_types() {
let fruit: Fruit = Fruit::Apple;
match fruit {
Fruit::Apple => println!("You're an Apple"),
Fruit::Bananas => println!("You're a Banana"),
}
}
/**
We can also not only match types, but also the values of that type.
Pattern matching also provide the functionality to return the value that has been matched.
We can use or operator [|] when we watch the type to collect multiple possible values.
We can also use range [n..=n] to define a possible range of number in the case.
*/
fn primitive_types() {
let mut rng = rand::thread_rng();
let num = rng.gen_range(0..4);
match num {
1 | 2 => println!("The number is 1 or 2"),
3 => println!("The number is 3"),
_ => println!("The number is not in the list"),
}
match num {
1..=3 => println!("The number is between 1 and 3"),
4 => println!("The number is 4"),
_ => println!("The number is not in the list"),
}
let string = "hello world";
match string {
"bye world" => println!("bye world"),
"hello world" => println!("hello world"),
"hello dude" => println!("hello dude"),
_ => {}
}
let value = match *Box::new(1) {
1 => "uno",
2 => "dos",
_ => "Empty"
};
println!("{}", value);
match 5 {
1..=5 => println!("The number is between 1 and 5"),
6 => println!("The number is 3"),
_ => println!("The number is not in the list"),
}
}
/**
Pattern matching it can match the [Option] Monad type, ans also unwrap the value of that monad.
[Option] can be [Some] or [None] and for the some type, we also extract the value
*/
fn option_type() {
let option: Option<String> = Some(String::from("hello pattern matching"));
match option {
Some(v) => println!("{}", v),
None => println!("No value found"),
}
}
/**
Pattern matching it can match the [Result] Monad type, ans also unwrap the value of that monad.
[Result] can be [Ok] or [Err] and for the some type, we also extract the value
*/
fn result_type() {
let result: Result<String, Error> = Ok(String::from("Success pattern matching"));
match result {
Ok(v) => println!("All good:{}", v),
Err(_) => {}
}
}
/**
Pattern matching it can also provide predicate function in cases to apart from match the type,
also do some filter by the value we extract.
Here apart from extract the value from the [Option] type, we also apply a filter over the value
*/
fn predicate_conditions() {
let head = rand::thread_rng().gen_range(0..2) == 0;
let message = String::from(if head { "Hello pattern matching with conditions" } else { "pattern matching" });
let option: Option<String> = Some(message);
match option {
Some(v) if v.contains("Hello") && v.len() > 5 => println!("{}", v),
None => println!("No value found"),
_ => println!("Some value Condition did not match")
}
}
/**
We can also apply pattern matching over a Struct attributes to folder over the same type, the attributes
we want to match.
Here we can use attributes [male] and [age] to filter multiple [Animals]
*/
fn struct_type() {
let animal = Animal { male: true, age: 5 };
match animal {
Animal { male: true, age: _age } => println!("Male Animal"),
Animal { male: _male, age: 10 } => println!("Old Animal"),
_ => println!("Animal does not match"),
}
}
/**
We can also use pattern matching to extract values from [Tuple] type
*/
fn tuple_type(){
let array = ("hello", "pattern", "matching","world");
match array {
(first , _, _, last) => println!("First:{} Last:{}",first, last ),
}
}
struct Animal {
male: bool,
age: i32,
}
enum Fruit {
Apple,
Bananas,
}