summaryrefslogtreecommitdiff
path: root/rust/rustlings/solutions/16_lifetimes/lifetimes3.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/rustlings/solutions/16_lifetimes/lifetimes3.rs')
-rw-r--r--rust/rustlings/solutions/16_lifetimes/lifetimes3.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/rust/rustlings/solutions/16_lifetimes/lifetimes3.rs b/rust/rustlings/solutions/16_lifetimes/lifetimes3.rs
new file mode 100644
index 0000000..16a5a68
--- /dev/null
+++ b/rust/rustlings/solutions/16_lifetimes/lifetimes3.rs
@@ -0,0 +1,18 @@
+// Lifetimes are also needed when structs hold references.
+
+struct Book<'a> {
+ // ^^^^ added a lifetime annotation
+ author: &'a str,
+ // ^^
+ title: &'a str,
+ // ^^
+}
+
+fn main() {
+ let book = Book {
+ author: "George Orwell",
+ title: "1984",
+ };
+
+ println!("{} by {}", book.title, book.author);
+}