0

I have a struct that looks like this:

#[derive(Default)]
pub struct Restaurant<'a> {
    name: String,
    patrons: Vec<Box<Patron>>,
    reservations: Vec<Box<Reservation<'a>>>
}

and then I have a function that does some stuff and creates a reservation object like so:

pub fn reserve_table(&self, ...) {
    let reservation = Box::new(Reservation {
            table: table_num,
            date: date,
            time: time,
            patron: p.unwrap()
    });

I now want to add this reservation variable into the struct's reservations vector. That vector should own the memory. I tried this

`self.reservations.push(reservation);`

But it doesn't work. It seems to think that the reservation local variable will die on the end of the function and so the vector would contain a hanging reference. But what I want is to transfer this variable to the reservations vector.

The Error

I experimented trying to make a private function like this:

fn add_reservation<'a>(&self<'a>, reservation: Box<Reservation><'a>) {
        self.reservations.push(reservation);
   }

but it didn't work. I think the syntax I used is wrong but i'd rather not have to use a seperate function to begin with.

I'm fairly new to rust and I'm not super familiar with lifetimes how do I do this?

nicholves
  • 1
  • 1

0 Answers0