summaryrefslogtreecommitdiff
path: root/exercises/16_lifetimes
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
committermo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
commit7123c7ae3a9605fbe962e4ef0a0f1424cd16fef8 (patch)
treec67f7e62bb9a179ae4fdbab492501cb6847e64c7 /exercises/16_lifetimes
parent77b687d501771c24bd83294d97b8e6f9ffa92d6b (diff)
parent4d9c346a173bb722b929f3ea3c00f84954483e24 (diff)
Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency
Diffstat (limited to 'exercises/16_lifetimes')
-rw-r--r--exercises/16_lifetimes/lifetimes1.rs23
-rw-r--r--exercises/16_lifetimes/lifetimes2.rs17
-rw-r--r--exercises/16_lifetimes/lifetimes3.rs15
3 files changed, 22 insertions, 33 deletions
diff --git a/exercises/16_lifetimes/lifetimes1.rs b/exercises/16_lifetimes/lifetimes1.rs
index 87bde49..19e2d39 100644
--- a/exercises/16_lifetimes/lifetimes1.rs
+++ b/exercises/16_lifetimes/lifetimes1.rs
@@ -1,15 +1,9 @@
-// lifetimes1.rs
-//
// The Rust compiler needs to know how to check whether supplied references are
// valid, so that it can let the programmer know if a reference is at risk of
// going out of scope before it is used. Remember, references are borrows and do
// not own their own data. What if their owner goes out of scope?
-//
-// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
-// hint.
-
-// I AM NOT DONE
+// TODO: Fix the compiler error by updating the function signature.
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() {
x
@@ -19,9 +13,16 @@ fn longest(x: &str, y: &str) -> &str {
}
fn main() {
- let string1 = String::from("abcd");
- let string2 = "xyz";
+ // You can optionally experiment here.
+}
- let result = longest(string1.as_str(), string2);
- println!("The longest string is '{}'", result);
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_longest() {
+ assert_eq!(longest("abcd", "123"), "abcd");
+ assert_eq!(longest("abc", "1234"), "1234");
+ }
}
diff --git a/exercises/16_lifetimes/lifetimes2.rs b/exercises/16_lifetimes/lifetimes2.rs
index 4f3d8c1..de5a5df 100644
--- a/exercises/16_lifetimes/lifetimes2.rs
+++ b/exercises/16_lifetimes/lifetimes2.rs
@@ -1,13 +1,4 @@
-// lifetimes2.rs
-//
-// So if the compiler is just validating the references passed to the annotated
-// parameters and the return type, what do we need to change?
-//
-// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
-// hint.
-
-// I AM NOT DONE
-
+// Don't change this function.
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
@@ -17,11 +8,13 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
}
fn main() {
+ // TODO: Fix the compiler error by moving one line.
+
let string1 = String::from("long string is long");
let result;
{
let string2 = String::from("xyz");
- result = longest(string1.as_str(), string2.as_str());
+ result = longest(&string1, &string2);
}
- println!("The longest string is '{}'", result);
+ println!("The longest string is '{result}'");
}
diff --git a/exercises/16_lifetimes/lifetimes3.rs b/exercises/16_lifetimes/lifetimes3.rs
index 9c59f9c..1cc2759 100644
--- a/exercises/16_lifetimes/lifetimes3.rs
+++ b/exercises/16_lifetimes/lifetimes3.rs
@@ -1,21 +1,16 @@
-// lifetimes3.rs
-//
// Lifetimes are also needed when structs hold references.
-//
-// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
-// hint.
-
-// I AM NOT DONE
+// TODO: Fix the compiler errors about the struct.
struct Book {
author: &str,
title: &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: "George Orwell",
+ title: "1984",
+ };
println!("{} by {}", book.title, book.author);
}