lifetime
Rust Store independently borrowed struct fields
A struct may need independent lifetime parameters when it stores references to data from different sources. The public type can expose those separate relationships rather than forcing every field into one common lifetime. Expected output (not executed): ready A9 Why: The copied shared reference in keep has its own relationship to id. Common mistake: Assuming every field extracted from a struct must have the same lifetime.
Formal Syntax & Structure
rust syntaxrust
struct Pair<'a, 'b> { first: &'a str, second: &'b str }Working Example
Example Snippetrust
struct Entry<'a, 'b> { id: &'a str, label: &'b str }fn main() { let id = String::from("A9"); let keep; { let label = String::from("ready"); let entry = Entry { id: &id, label: &label }; println!("{}", entry.label); keep = entry.id; } println!("{keep}");}