-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOption.rs
More file actions
48 lines (40 loc) · 875 Bytes
/
Copy pathOption.rs
File metadata and controls
48 lines (40 loc) · 875 Bytes
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
/*
Structs and Option enum.
*/
struct Student{
name:String,
grade:Option<u32>,
}
fn get_grades(stud_name:String, student_db:&Vec<Student>) -> Option<u32> {
for _student @ ref sref in student_db{
if sref.name == stud_name{
return sref.grade;
}
}
None
}
fn main(){
let student_db:Vec<Student> = vec![
Student{
name:String::from("RamLal"),
grade:Some(85),
},
Student{
name:String::from("ShamLal"),
grade:Some(60),
},
Student{
name:String::from("HariLal"),
grade:None,
},
];
let grade = get_grades("ShamLal".to_string(), &student_db);
match grade{
Some(grade) => println!("The grade is {}", grade),
None=> {},
}
}
// enum Option<T>{
// None,
// Some(T),
// }