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.
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
: A Vec<u8>
to store the student's grades.Implement the following methods for the StudentGrades
struct:
add_student(name: &str)
: Add a new student to the HashMap
. 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.HashMap
methods like entry
, insert
, and get
to manage student data.