mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-11-08 09:09:17 +01:00
lifetimes3 solution
This commit is contained in:
parent
275a854d6e
commit
6187216606
@ -1,16 +1,15 @@
|
|||||||
// Lifetimes are also needed when structs hold references.
|
// Lifetimes are also needed when structs hold references.
|
||||||
|
|
||||||
|
// TODO: Fix the compiler errors about the struct.
|
||||||
struct Book {
|
struct Book {
|
||||||
author: &str,
|
author: &str,
|
||||||
title: &str,
|
title: &str,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let name = String::from("Jill Smith");
|
|
||||||
let title = String::from("Fish Flying");
|
|
||||||
let book = Book {
|
let book = Book {
|
||||||
author: &name,
|
author: "George Orwell",
|
||||||
title: &title,
|
title: "1984",
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("{} by {}", book.title, book.author);
|
println!("{} by {}", book.title, book.author);
|
||||||
|
@ -843,9 +843,7 @@ inner block:
|
|||||||
name = "lifetimes3"
|
name = "lifetimes3"
|
||||||
dir = "16_lifetimes"
|
dir = "16_lifetimes"
|
||||||
test = false
|
test = false
|
||||||
hint = """
|
hint = """Let the compiler guide you :)"""
|
||||||
If you use a lifetime annotation in a struct's fields, where else does it need
|
|
||||||
to be added?"""
|
|
||||||
|
|
||||||
# TESTS
|
# TESTS
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user