I am trying to make a simple struct, which contains WeightedChoice from rand crate, but I am having lifetime troubles:
pub struct UnigramNegativeSampler<'a> {
distribution: WeightedChoice<'a, usize>,
choices: Vec<Weighted<usize>>
}
impl<'a> UnigramNegativeSampler<'a> {
pub fn new(counts: &[usize]) -> UnigramNegativeSampler {
let mut choices = counts.iter().enumerate().map(|(index, count)| {
Weighted { weight: *count as u32, item: index }
}).collect::<Vec<_>>();
UnigramNegativeSampler { distribution: WeightedChoice::new(&mut choices), choices }
}
}
However I am getting lifetime error:
error[E0597]: `choices` does not live long enough
--> src/lib.rs:83:73
|
83 | UnigramNegativeSampler { distribution: WeightedChoice::new(&mut choices), choices }
| ^^^^^^^ borrowed value does not live long enough
84 | }
Is there any way around this? Or am I hitting a wall with self referential structs.