2022-07-15 12:01:32 +00:00
|
|
|
// lifetimes3.rs
|
|
|
|
//
|
|
|
|
// Lifetimes are also needed when structs hold references.
|
|
|
|
//
|
2023-05-29 17:39:08 +00:00
|
|
|
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
|
|
|
|
// hint.
|
2022-07-15 12:01:32 +00:00
|
|
|
|
2023-09-10 16:38:07 +00:00
|
|
|
struct Book<'a> {
|
|
|
|
author: &'a str,
|
|
|
|
title: &'a str,
|
2022-07-15 12:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let name = String::from("Jill Smith");
|
|
|
|
let title = String::from("Fish Flying");
|
2023-09-10 16:38:07 +00:00
|
|
|
let book = Book {
|
|
|
|
author: &name,
|
|
|
|
title: &title,
|
|
|
|
};
|
2022-07-15 12:01:32 +00:00
|
|
|
|
|
|
|
println!("{} by {}", book.title, book.author);
|
|
|
|
}
|