mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-13 00:00:03 +03:00
Complete lifetimes exercises
This commit is contained in:
parent
e2672ba060
commit
5e3230c931
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
println!("The longest string is '{}'", result);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue