Understanding a lifetime error

⚓ Rust    📅 2026-03-01    👤 surdeus    👁️ 1      

surdeus

I have a structure RolodexBase (used in the state structure for iced) which contains a HashMap (people) which maps usize to Person (another structure, owned by the RolodexBased). That has a method get_person to get a reference to a specific person by the usize key.

    |
121 |     pub fn get_person<'a>(&self, id: usize) -> Option<&'a Person> {
    |                       --  - let's call the lifetime of this reference `'1`
    |                       |
    |                       lifetime `'a` defined here
122 |         self.people.get(&id)
    |         ^^^^^^^^^^^^^^^^^^^^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`

Given that the HashMap and its Person(s) have the same lifetime as the RolodexBase, I thought I could use the same lifetime for the result. Apparently, I am missing something that should have been obvious to me. Sorry.
Joel

4 posts - 3 participants

Read full topic

🏷️ Rust_feed