Hi all! First post here, started with my first Rust experiment a few days ago and now I’m struggling with lifetimes.
I want to have a HashMap with a struct (chrono::NaiveDate
) as key and another struct (DayDescriptor
) as value. The value struct DayDescriptor
shall keep a reference to the key struct.
This is what I tried:
struct DayDescriptor<'a> {
naive: &'a NaiveDate,
}
pub struct Calendar <'a> {
today: NaiveDate,
focusday: NaiveDate,
days: HashMap<NaiveDate, DayDescriptor<'a>>
}
impl Calendar {
pub fn new(today: &NaiveDate, focusday:&NaiveDate) -> Calendar {
let mut calendar = Calendar {
days: HashMap::new()
};
calendar
}
Well, the compiler of course claims a missing lifetime specification when I try to create an instance of Calendar
. What I would like to tell the compiler is that the lifetime 'a
must be the lifetime of the maps key. Does that make sense? How can I do that? Or how can I at least use the lifetime of the HashMap
or the Calendar
struct itself? I don’t want to bother the “user” of this struct (me - tomorrow) with some lifetime stuff if the lifetime can be specified within the struct.
Thanks!