What is the best practice in Rust for handling lifetimes within a hierarchy of structs? For example, should I take this hierarchy:
struct Content {
type_: String,
value: String,
}
struct Contact {
email: String,
name: String,
}
struct Personalization {
to: Vec<Contact>
}
struct Body {
personalizations: Vec<Personalization>,
from: Contact,
subject: String,
content: Vec<Content>
}
and rewrite it as:
struct Content<'t, 'v> {
type_: &'t str,
value: &'v str,
}
struct Contact<'e, 'n> {
email: &'e str,
name: &'n str,
}
struct Personalization<'t, 'n, 'e> {
to: &'t [Contact<'n, 'e>] // ?
}
struct Body<'p, 't, 'n, 'e, 't2, 'v, 's, 'c, 't3, 'v2> {
personalizations: &'p [Personalization<'t, 'n, 'e>], // ?
from: Contact<'t2, 'v>,
subject: &'s str,
content: &'c [Content<'t3, 'v2>] // ?
}
I feel like the answer is no, but also yes if I want to avoid unnecessary allocations.
- For the slices, is each element forced to have the same lifetime?
- What are the best practices for handling lifetimes in structs in these situations?