'Use field in value as key [duplicate]

I am trying to implement something like

struct Student {
    name: String,
    gpa: f32,
    // ...
}

struct StudentList {
    items: HashMap<&String, Student>,
}

impl StudentList {
    pub fn set(&mut self, student: Student) {
        self.items.insert(student.name, student)
    }
}

Yet the issue is that if I use &String I need to provide a lifetime. How do I specify the key has the same lifetime as the value?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source