Skip to content

Commit

Permalink
Add lifetimes 1 - 3
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldww committed May 2, 2023
1 parent 4ec23be commit 3d7dc16
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 1 addition & 3 deletions exercises/lifetimes/lifetimes1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,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 {
Expand Down
5 changes: 2 additions & 3 deletions exercises/lifetimes/lifetimes2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,8 +17,9 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
fn main() {
let string1 = String::from("long string is long");
let result;
let string2;
{
let string2 = String::from("xyz");
string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is '{}'", result);
Expand Down
13 changes: 7 additions & 6 deletions exercises/lifetimes/lifetimes3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,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");
let book = Book { author: &name, title: &title };
let book = Book {
author: &name,
title: &title,
};

println!("{} by {}", book.title, book.author);
}

0 comments on commit 3d7dc16

Please sign in to comment.