-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatch.rs
62 lines (54 loc) · 1.14 KB
/
match.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
#![allow(unused)]
enum Animal {
Cat,
Dog,
Duck,
Mouse,
}
fn main() {
// Match
let x = 1;
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
_ => println!("other"),
}
// Multiple
match x {
1 | 2 | 3 => println!("1 or 2 or 3"),
_ => println!("other"),
}
// Range
match x {
1..=10 => println!("between 1 to 10"),
_ => println!("other"),
}
// @
match x {
i @ 1..=10 => println!("@ {i}"),
_ => println!("other"),
}
// Assigning value from match
let animal = Animal::Cat;
let animal_sound = match animal {
Animal::Cat => "meow",
Animal::Dog => "woof",
Animal::Duck => "quack",
_ => "?",
};
println!("{}", animal_sound);
// Option
let x: Option<u32> = Some(3);
match x {
Some(x) => println!("{x}"),
None => println!("none"),
}
// Result
let res: Result<u32, String> = Ok(1);
match res {
Ok(i) => println!("ok {i}"),
// Ignore error message
Err(_) => println!("err"),
}
}