-
Notifications
You must be signed in to change notification settings - Fork 0
/
42-pattern-matching.rs
75 lines (55 loc) · 1.57 KB
/
42-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
fn main() {
let favorite_color: Option<&str> = None;
let is_tuesday = false;
let age: Result<u8, _> = "34".parse();
// match
match is_tuesday {
true => println!("Using purple as background color"),
false => println!("Using orange as background color"),
_ => println!("Using black as background color")
};
// if let
if let Some(color) = favorite_color {
println!("Using your favorite color {:?}", favorite_color)
} else if is_tuesday {
println!("Tuesday is green day");
} else if let Ok(age) = age {
if age > 30 {
println!("Using purple as background color");
} else {
println!("Using orange as background color");
}
} else {
println!("Using blue as background color");
};
// while let
let mut stack = Vec::new();
stack.push(1);
stack.push(2);
stack.push(3);
while let Some(top) = stack.pop() {
println!("{}", top);
}
// for let
let v = vec!["a", "b", "c"];
for (index, value) in v.iter().enumerate() {
println!("{} is at index {}", value, index)
}
// Assignment
let (x, y, z) = (1,2,3);
println!("{} {} {}", x,y,z);
// sytax
let x = 1;
match x {
1|2 => println!("hey!"),
3...5 => println!("hey?"),
_ => println!("bye ... ")
}
// Ref and Ref mut
let mut name = Some(String::from("Bob")) ;
match name {
Some(ref mut name) => *name = String::from("John"),
None => (),
};
println!("{:?}", name);
}