If you’ve completed the first part of the Student Grades Tracker challenge, congratulations! Now it’s time to enhance the functionality by adding methods directly to the Student struct.
This will help you practice designing methods for individual struct instances while integrating them into a larger system.
For this challenge, we’ll build upon the StudentGrades system. In addition to the methods you’ve already implemented, you’ll now focus on adding two new methods to the Student struct itself.
Your Task
You need to extend the Student struct by implementing two methods:
add_grade: Add a grade to the student’sgradesvector.average_grade(&self) -> f64: Calculate and return the average grade of the student. Return0.0if the student has no grades.
The StudentGrades struct remains the same as in the previous challenge. You’ll modify its existing methods to use the newly added methods of the Student struct.
use std::collections::HashMap;pub struct Student {pub name: String,pub grades: Vec<u8>,}impl Student {pub fn add_grade(&mut self, grade: u8) {// Implement here}pub fn average_grade(&self) -> f64 {// Implement here}}pub struct StudentGrades {pub students: HashMap<String, Student>,}impl StudentGrades {pub fn new() -> Self {Self {students: HashMap::new(),}}pub fn add_student(&mut self, name: &str) {self.students.entry(name.to_string()).or_insert(Student {name: name.to_string(),grades: vec![],});}pub fn add_grade(&mut self, name: &str, grade: u8) {if let Some(student) = self.students.get_mut(name) {student.add_grade(grade);}}pub fn get_grades(&self, name: &str) -> &[u8] {&self.students.get(name).unwrap().grades}}pub fn main() {let mut tracker = StudentGrades::new();tracker.add_student("Alice");tracker.add_student("Bob");tracker.add_grade("Alice", 85);tracker.add_grade("Alice", 90);tracker.add_grade("Bob", 78);let alice = tracker.students.get_mut("Alice").unwrap();alice.add_grade(95);println!("{:?}", alice.grades);println!("{:?}", alice.average_grade());println!("{:?}", tracker.get_grades("Bob"));}