Store references in structs
Quick answer
A struct can borrow data instead of owning a copy. Lifetime parameters on its type express how long that borrowed data must remain valid.
Borrowed fields and methods
struct Label<'a> { text: &'a str } stores a borrowed string slice. The struct's usable lifetime cannot exceed the borrowed data's validity. In impl<'a> Label<'a>, the lifetime is declared for the implementation; methods may return a reference tied to self or explicitly to the stored lifetime.
struct Label<'a> { text: &'a str }impl<'a> Label<'a> { fn text(&self) -> &str { self.text } }fn main() { let owner = String::from("draft"); let label = Label { text: &owner }; println!("{}", label.text());}Expected stdout (not executed):
draftNo additional string allocation is needed for the field.
struct Label<'a> { text: &'a str }fn main() { let label; { let owner = String::from("draft"); label = Label { text: &owner }; } println!("{}", label.text);}Expected: compilation fails; exact diagnostic not verified.
Use an owned String field if the label must survive the source.
Common mistake
A borrowed struct is not automatically self-contained; its fields can depend on external data.
Try it yourself
Create a borrowed Article with two text fields, then compare with an owned version.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Store references in structs — practice quiz
Choose one answer per question, then review the explanation.
Does `struct View<'a> { text: &'a str }` own its string bytes?
Which implementation header declares the lifetime for View<'a>?
What must outlive a use of a View holding a borrowed String slice?