I am trying to set the lifetime on a new vector of a particular type. I understand about lifetimes and borrowing. That is not the problem. The problem is the actual syntax to set the lifetime to <'a>.
I keep getting an error about the vector not living long enough, but when I try and set the lifetime I get a different error. Here is what my code looks like.
#[derive(Clone, Copy, Debug)]
pub struct ProfessorGroup<'a> {
name: &'a str,
gender: Gender,
professors: &'a Vec<Professor<'a>>,
rank: ProfessorRank,
attrition_rate:f64,
promotion_rate:f64,
hiring_rate:f64,
current_number:i32,
}
impl<'a> Default for ProfessorGroup<'a>{
fn default() -> ProfessorGroup<'a>{
ProfessorGroup {
name: "uninitialized group",
gender: Gender::Female,
professors:&mut Vec<'a>::<Professor<'a>>::new(),//PROBLEM CODE
rank: ProfessorRank::Assistant,
attrition_rate: 0.0,
promotion_rate: 0.0,
hiring_rate: 0.0,
current_number: 0,
}
}
}
The error I am getting is:
error: expected `:`, found `>`
--> src/Actors/ProfessorGroups.rs:21:35
|
21 | professors:&mut Vec<'a>::<Professor<'a>>::new(),
| ^
error[E0063]: missing fields `attrition_rate`, `current_number`, `hiring_rate` and 3 other fields in initializer of `Actors::ProfessorGroups::ProfessorGroup<'_>`
The error seems to kill access to the fields below--hence the missing fields comment.
I tried professors:&mut <'a>Vec::<Professor<'a>>::new(),
but that gave the same error.
I took out the lifetime altogether professors:&mut Vec::<Professor<'a>>::new(),
but that just gave me an error that the vector was not living long enough.
I looked through the documentation but the closest I found was something like this, which did not work either: https://users.rust-lang.org/t/why-cant-i-specify-type-parameters-directly-after-the-type/2365
Can anyone see where I am making an error in the syntax?