-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.rs
36 lines (30 loc) 路 1022 Bytes
/
option.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
fn main() {
// Vec::get method returns Option type
let fruits = vec!["banana", "apple", "coconut", "orange", "strawberry"];
// pick the first item:
let first = fruits.get(0);
println!("{:?}", first);
// pick the third item:
let third = fruits.get(2);
println!("{:?}", third);
// pick the 99th item, which is non-existent:
let non_existent = fruits.get(99);
println!("{:?}", non_existent);
// Pattern matching
let fruits = vec!["banana", "apple", "coconut", "orange", "strawberry"];
for &index in [0, 2, 99].iter() {
match fruits.get(index) {
Some(fruit_name) => println!("It's a delicious {}!", fruit_name),
None => println!("There is no fruit! :("),
}
}
// `if let` expression
let some_number: Option<u8> = Some(7);
match some_number {
Some(7) => println!("That's my lucky number!"),
_ => {},
}
if let Some(7) = some_number {
println!("That's my lucky number!");
}
}