Student Grades Tracker
Now that we know about vectors and hashmaps, let’s tackle something more challenging. In this exercise, we’ll build a system to manage and analyze student grades. This challenge will help you practice working with structs, hashmaps, vectors, and methods while managing a collection of data.
The focus here is on functionality. Ignore error handling for this challenge; assume the input is always valid.
Your Task
You need to define the StudentGrades struct, which contains a HashMap of student names (String) as keys and Student structs as values. Each Student struct should have the following fields:
name: The name of the student (String).grades: AVec<u8>to store the student's grades.
Implement the following methods for the StudentGrades struct:
add_student(name: &str): Add a new student to theHashMap. If the student already exists, do nothing.add_grade(name: &str, grade: u8): Add a grade to an existing student.get_grades(name: &str) -> &[u8]: Retrieve the grades of a student as an immutable reference.- Since there’s no error handling, assume all inputs are valid, and keys will exist when accessed.
Hints
Click here to reveal hints
- Use the
HashMapmethods likeentry,insert, andgetto manage student data.
No run yet
Press
Run to execute your code and see test results here.