Hi all,
I'm quite new to Rust and, despite I've been reading thoughtfully the Rust Programming language
and Programming in Rust
books, I'm still struggling with lifetimes. This is a simplification of a code I'm currently working on:
pub struct G {
pub name: String,
pub p: &P,
}
impl G {
pub fn new(name: String, p: &P) -> G {
G {
name: name,
p: p,
}
}
}
pub struct S {
pub gs: Vec<G>,
}
impl S {
pub fn new() -> S {
S {
gs: Vec::new(),
}
}
pub fn add_gs(&mut self, i: u32, p: &P) {
let g = G::new(format!("g_{}", i));
self.gs.push(g);
}
}
pub struct P {
pub big: Vec<i32>,
}
impl P {
pub fn new() -> P {
P {
big: Vec::with_capacity(100000 as usize),
}
}
}
fn main() {
let p = P::new();
let mut s = S::new();
for i in 1..5 {
s.add_gs(i, &p);
}
}
Basically, the idea is to save unnecessary copy()'s of a large chunk of data (big
).
I'm asking for your help to understand which lifetime logic could make this code working properly (if there is any) as I've tried without success.
Thanks in advance for your help, time and patience,
Brian