mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 13:49:19 +00:00
feat: 🎸 added solutions for lifetime exercises
This commit is contained in:
parent
1f2f1d8ba6
commit
bf297f6f60
@ -7,9 +7,7 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a 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 hint.
|
// 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 {
|
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||||
if x.len() > y.len() {
|
if x.len() > y.len() {
|
||||||
x
|
x
|
||||||
@ -18,10 +16,9 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let string1 = String::from("long string is long");
|
let string1 = String::from("long string is long");
|
||||||
let result;
|
|
||||||
{
|
{
|
||||||
let string2 = String::from("xyz");
|
let string2 = String::from("xyz");
|
||||||
result = longest(string1.as_str(), string2.as_str());
|
let result = longest(string1.as_str(), string2.as_str());
|
||||||
}
|
|
||||||
println!("The longest string is '{}'", result);
|
println!("The longest string is '{}'", result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,17 +4,18 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
struct Book<'a> {
|
||||||
|
author: &'a str,
|
||||||
struct Book {
|
title: &'a str,
|
||||||
author: &str,
|
|
||||||
title: &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");
|
||||||
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…
x
Reference in New Issue
Block a user