lifetimes3.rs (348B)
1 // Lifetimes are also needed when structs hold references. 2 3 struct Book<'a> { 4 // ^^^^ added a lifetime annotation 5 author: &'a str, 6 // ^^ 7 title: &'a str, 8 // ^^ 9 } 10 11 fn main() { 12 let book = Book { 13 author: "George Orwell", 14 title: "1984", 15 }; 16 17 println!("{} by {}", book.title, book.author); 18 }