-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterators.rs
More file actions
55 lines (46 loc) · 1000 Bytes
/
Iterators.rs
File metadata and controls
55 lines (46 loc) · 1000 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
48
49
50
51
52
53
54
55
/*
Iterators
*/
// Iterator{
// type Item;
// fn next(&mut self) -> Option<Self::Item>
// }
struct Employee{
name:String,
age:u32,
}
struct EmployeeRecords{
emp_db:Vec<Employee>,
}
impl Iterator for EmployeeRecords{
type Item = String;
fn next(&mut self) -> Option<Self::Item>{
if self.emp_db.len() != 0{
let res:String = self.emp_db[0].name.clone();
self.emp_db.remove(0);
Some(res)
}
else{
None
}
}
}
fn main(){
let e1: Employee = Employee{
name:String::from("RamLal"),
age:30,
};
let e2: Employee = Employee{
name:String::from("ShamLal"),
age:10,
};
let mut emp_db:EmployeeRecords = EmployeeRecords{
emp_db:vec![e1,e2],
};
// println!("{:?}", emp_db.next());
// println!("{:?}", emp_db.next());
// println!("{:?}", emp_db.next());
for employee in emp_db{
println!("{}", employee);
}
}