Complete lifetimes exercises

This commit is contained in:
Suzie Kim 2024-02-02 14:31:41 -05:00
parent e2672ba060
commit 5e3230c931
No known key found for this signature in database
GPG key ID: 83C4CC3808F9AAE9
3 changed files with 8 additions and 10 deletions

View file

@ -8,9 +8,7 @@
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
fn longest(x: &str, y: &str) -> &str {
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {

View file

@ -6,8 +6,6 @@
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
@ -22,6 +20,6 @@ fn main() {
{
let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is '{}'", result);
}
}

View file

@ -5,16 +5,18 @@
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
struct Book {
author: &str,
title: &str,
struct Book<'a> {
author: &'a str,
title: &'a str,
}
fn main() {
let name = String::from("Jill Smith");
let title = String::from("Fish Flying");
// The Book struct is returning two parameters
// so it must specify the lifetime of both references.
let book = Book { author: &name, title: &title };
println!("{} by {}", book.title, book.author);