How to Use Lifetimes with Impl Blocks and Methods

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"
    }
}