Good Code
The good version returns a reference to an element already owned by the input slice. The lifetime in the signature documents that relationship.
Lesson 02
Return borrowed data only when the lifetime is tied to an input, instead of returning references to local values.
pub fn first_approved<'a>(reviews: &'a [Review]) -> Option<&'a Review> {
// The returned reference can only live as long as the input slice.
reviews.iter().find(|review| review.approved)
}pub fn first_approved_title(reviews: &[Review]) -> &str {
let title = reviews
.iter()
.find(|review| review.approved)
.map(|review| review.title.clone())
.unwrap_or_default();
// Returning a reference to local storage cannot outlive this function.
title.as_str()
}The good version returns a reference to an element already owned by the input slice. The lifetime in the signature documents that relationship.
The bad version builds a local String and tries to return a borrowed &str from it. That reference would point to storage dropped at the end of the function.