Skip to content

Commit 21ecb34

Browse files
fix: validation update fields
1 parent 68ae0c7 commit 21ecb34

3 files changed

Lines changed: 45 additions & 21 deletions

File tree

rust_session/student_registry/src/main.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ use registry::Registry;
99
use student_struct::Student;
1010

1111
fn main() {
12+
fn print_separator() {
13+
println!(" ");
14+
println!("==========================================================");
15+
println!("==========================================================");
16+
println!(" ");
17+
}
18+
1219
let sex = Sex::Male;
1320
println!("sex: {:?}", sex.to_str());
1421

@@ -30,13 +37,20 @@ fn main() {
3037
registry.add("Mark", 20, Sex::Male, Grade::First, 72.0);
3138
registry.add("Janet", 20, Sex::Female, Grade::First, 82.0);
3239
let custom_id = registry.students[2].id;
40+
print_separator();
3341
registry.list_all();
3442
let updated_student: Student =
35-
Student::new(3, "Fin".to_string(), 18, Sex::Female, Grade::Third, 40.5);
36-
registry.update_by_index(2, updated_student);
43+
Student::new(3, "Fin".to_string(), 17, Sex::Female, Grade::First, 12.0);
44+
print_separator();
45+
registry.update(3, updated_student);
46+
print_separator();
3747
registry.list_all();
48+
print_separator();
3849
registry.delete(3);
50+
print_separator();
51+
registry.list_all();
3952
registry.delete(custom_id);
53+
print_separator();
4054
registry.list_all();
4155
}
4256

@@ -69,7 +83,6 @@ fn main() {
6983
// registry.list_all();
7084
// let updated_student: Student =
7185
// Student::new("Fin".to_string(), 18, Sex::Female, Grade::Third, 40.5);
72-
// registry.update_by_index(2, updated_student);
7386
// registry.list_all();
7487
// registry.delete(custom_id);
7588
// }

rust_session/student_registry/src/registry.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,26 @@ impl Registry {
5353
}
5454

5555
pub fn update(&mut self, id: u32, student: Student) {
56-
match self.students.iter_mut().find(|s| s.id == id) {
57-
Some(s30) => {
58-
*s = student;
59-
}
60-
None => println!("Student with ID {} not found", id),
56+
let Some(s) = self.students.iter().find(|s| s.id == id) else {
57+
println!("Student with ID {} not found", id);
58+
return;
59+
};
60+
61+
if student.name == s.name
62+
&& student.age == s.age
63+
&& student.grade == s.grade
64+
&& student.score == s.score
65+
{
66+
println!("No updates found, cannot make update.");
67+
return;
6168
}
62-
}
6369

64-
pub fn update_by_index(&mut self, index: usize, student: Student) {
65-
match self.students.get_mut(index) {
70+
match self.students.iter_mut().find(|s| s.id == id) {
6671
Some(s) => {
6772
*s = student;
73+
println!("Student with ID {} updated", id);
6874
}
69-
None => println!("Student at index {} not found", index),
75+
None => println!("Student with ID {} not found", id),
7076
}
7177
}
7278

rust_session/student_registry/src/registry_uuid.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,25 @@ impl Registry {
5454
}
5555

5656
pub fn update(&mut self, id: Uuid, student: Student) {
57-
match self.students.iter_mut().find(|s| s.id == id) {
58-
Some(s) => {
59-
*s = student;
60-
}
61-
None => println!("Student with ID {} not found", id),
57+
let Some(s) = self.students.iter().find(|s| s.id == id) else {
58+
println!("Student with ID {} not found", id);
59+
return;
60+
};
61+
62+
if student.name == s.name
63+
|| student.age == s.age
64+
|| student.grade == s.grade
65+
|| student.score == s.score
66+
{
67+
println!("No updates found, cannot make update.");
68+
return;
6269
}
63-
}
6470

65-
pub fn update_by_index(&mut self, index: usize, student: Student) {
66-
match self.students.get_mut(index) {
71+
match self.students.iter_mut().find(|s| s.id == id) {
6772
Some(s) => {
6873
*s = student;
6974
}
70-
None => println!("Student at index {} not found", index),
75+
None => println!("Student with ID {} not found", id),
7176
}
7277
}
7378

0 commit comments

Comments
 (0)