Skip to content

Commit c3d05da

Browse files
committed
lifetimes
1 parent d87472e commit c3d05da

File tree

5 files changed

+20
-28
lines changed

5 files changed

+20
-28
lines changed

exercises/iterators/iterators1.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
1010
// hint.
1111

12-
// I AM NOT DONE
13-
1412
fn main() {
1513
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
1614

17-
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
15+
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
1816

1917
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
20-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
18+
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
2119
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
22-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
20+
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
2321
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
24-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
25-
}
22+
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
23+
}

exercises/lifetimes/lifetimes1.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
99
// hint.
1010

11-
// I AM NOT DONE
12-
13-
fn longest(x: &str, y: &str) -> &str {
11+
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
12+
// fn longest(x: &str, y: &str) -> &str {
1413
if x.len() > y.len() {
1514
x
1615
} else {
@@ -24,4 +23,4 @@ fn main() {
2423

2524
let result = longest(string1.as_str(), string2);
2625
println!("The longest string is '{}'", result);
27-
}
26+
}

exercises/lifetimes/lifetimes2.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
77
// hint.
88

9-
// I AM NOT DONE
10-
119
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1210
if x.len() > y.len() {
1311
x
@@ -22,6 +20,6 @@ fn main() {
2220
{
2321
let string2 = String::from("xyz");
2422
result = longest(string1.as_str(), string2.as_str());
23+
println!("The longest string is '{}'", result);
2524
}
26-
println!("The longest string is '{}'", result);
27-
}
25+
}

exercises/lifetimes/lifetimes3.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
9-
10-
struct Book {
11-
author: &str,
12-
title: &str,
8+
struct Book<'a> {
9+
author: &'a str,
10+
title: &'a str,
1311
}
1412

1513
fn main() {
@@ -18,4 +16,4 @@ fn main() {
1816
let book = Book { author: &name, title: &title };
1917

2018
println!("{} by {}", book.title, book.author);
21-
}
19+
}

exercises/quiz3.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
//
1717
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
1818

19-
// I AM NOT DONE
20-
21-
pub struct ReportCard {
22-
pub grade: f32,
19+
pub struct ReportCard<T> {
20+
pub grade: T,
2321
pub student_name: String,
2422
pub student_age: u8,
2523
}
2624

27-
impl ReportCard {
25+
use std::fmt::Display;
26+
impl<T: Display> ReportCard<T> {
2827
pub fn print(&self) -> String {
2928
format!("{} ({}) - achieved a grade of {}",
3029
&self.student_name, &self.student_age, &self.grade)
@@ -52,7 +51,7 @@ mod tests {
5251
fn generate_alphabetic_report_card() {
5352
// TODO: Make sure to change the grade here after you finish the exercise.
5453
let report_card = ReportCard {
55-
grade: 2.1,
54+
grade: String::from("A+"),
5655
student_name: "Gary Plotter".to_string(),
5756
student_age: 11,
5857
};
@@ -61,4 +60,4 @@ mod tests {
6160
"Gary Plotter (11) - achieved a grade of A+"
6261
);
6362
}
64-
}
63+
}

0 commit comments

Comments
 (0)