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
|
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||||
|
|
||||||
fn longest(x: &str, y: &str) -> &str {
|
|
||||||
if x.len() > y.len() {
|
if x.len() > y.len() {
|
||||||
x
|
x
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -6,8 +6,6 @@
|
||||||
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||||
if x.len() > y.len() {
|
if x.len() > y.len() {
|
||||||
x
|
x
|
||||||
|
@ -22,6 +20,6 @@ fn main() {
|
||||||
{
|
{
|
||||||
let string2 = String::from("xyz");
|
let string2 = String::from("xyz");
|
||||||
result = longest(string1.as_str(), string2.as_str());
|
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
|
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct Book {
|
struct Book<'a> {
|
||||||
author: &str,
|
author: &'a str,
|
||||||
title: &str,
|
title: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let name = String::from("Jill Smith");
|
let name = String::from("Jill Smith");
|
||||||
let title = String::from("Fish Flying");
|
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 };
|
let book = Book { author: &name, title: &title };
|
||||||
|
|
||||||
println!("{} by {}", book.title, book.author);
|
println!("{} by {}", book.title, book.author);
|
||||||
|
|
Loading…
Reference in a new issue