Add a lifetime parameter to the impl block and method signature to link the returned reference's validity to the struct instance.
Add a lifetime parameter to the impl block and the method signature to tell the compiler how long the reference returned by the method lives relative to the struct instance.
struct ImportantExcerpt<'a> {
part: &'a str,
}
impl<'a> ImportantExcerpt<'a> {
fn level(&'a self) -> &'a str {
"Critical"
}
}
Lifetimes in impl blocks act like expiration dates for references, ensuring the compiler knows how long a piece of data is valid before it gets deleted. Think of it like a library book checkout: the impl block sets the rules for how long you can keep the book (the reference) based on how long you own the library card (the struct). You use this whenever a method returns a reference to data that lives inside the struct itself.