From 61872166067ab83fbad87e55c562e28d98368bff Mon Sep 17 00:00:00 2001 From: mo8it Date: Thu, 27 Jun 2024 16:15:53 +0200 Subject: [PATCH] lifetimes3 solution --- exercises/16_lifetimes/lifetimes3.rs | 7 +++---- rustlings-macros/info.toml | 4 +--- solutions/16_lifetimes/lifetimes3.rs | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/exercises/16_lifetimes/lifetimes3.rs b/exercises/16_lifetimes/lifetimes3.rs index 9b631cab..1cc27592 100644 --- a/exercises/16_lifetimes/lifetimes3.rs +++ b/exercises/16_lifetimes/lifetimes3.rs @@ -1,16 +1,15 @@ // Lifetimes are also needed when structs hold references. +// TODO: Fix the compiler errors about the struct. struct Book { author: &str, title: &str, } fn main() { - let name = String::from("Jill Smith"); - let title = String::from("Fish Flying"); let book = Book { - author: &name, - title: &title, + author: "George Orwell", + title: "1984", }; println!("{} by {}", book.title, book.author); diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index e2ebfa5c..f0e34a5e 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -843,9 +843,7 @@ inner block: name = "lifetimes3" dir = "16_lifetimes" test = false -hint = """ -If you use a lifetime annotation in a struct's fields, where else does it need -to be added?""" +hint = """Let the compiler guide you :)""" # TESTS diff --git a/solutions/16_lifetimes/lifetimes3.rs b/solutions/16_lifetimes/lifetimes3.rs index 4e181989..16a5a684 100644 --- a/solutions/16_lifetimes/lifetimes3.rs +++ b/solutions/16_lifetimes/lifetimes3.rs @@ -1 +1,18 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +// 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); +}