Skip to content

Commit 242cd3f

Browse files
committed
30/30
1 parent 7cc0b44 commit 242cd3f

File tree

17 files changed

+64
-80
lines changed

17 files changed

+64
-80
lines changed

exercises/generics/generics1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
77
// hint.
88

9-
// I AM NOT DONE
10-
119
fn main() {
12-
let mut shopping_list: Vec<?> = Vec::new();
10+
let mut shopping_list: Vec<&str> = Vec::new();
1311
shopping_list.push("milk");
1412
}

exercises/generics/generics2.rs

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

9-
// I AM NOT DONE
10-
11-
struct Wrapper {
12-
value: u32,
9+
struct Wrapper<T> {
10+
value: T,
1311
}
1412

15-
impl Wrapper {
16-
pub fn new(value: u32) -> Self {
13+
impl <T> Wrapper <T> {
14+
pub fn new(value: T) -> Self {
1715
Wrapper { value }
1816
}
1917
}

exercises/iterators/iterators1.rs

Lines changed: 4 additions & 6 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
22+
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
2523
}

exercises/iterators/iterators2.rs

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

9-
// I AM NOT DONE
10-
119
// Step 1.
1210
// Complete the `capitalize_first` function.
1311
// "hello" -> "Hello"
1412
pub fn capitalize_first(input: &str) -> String {
1513
let mut c = input.chars();
1614
match c.next() {
1715
None => String::new(),
18-
Some(first) => ???,
16+
Some(first) => first.to_uppercase().collect::<String>() + c.as_str(),
1917
}
2018
}
2119

@@ -24,15 +22,18 @@ pub fn capitalize_first(input: &str) -> String {
2422
// Return a vector of strings.
2523
// ["hello", "world"] -> ["Hello", "World"]
2624
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
27-
vec![]
25+
words.iter().map(|word| capitalize_first(word)).collect()
2826
}
2927

3028
// Step 3.
3129
// Apply the `capitalize_first` function again to a slice of string slices.
3230
// Return a single string.
3331
// ["hello", " ", "world"] -> "Hello World"
3432
pub fn capitalize_words_string(words: &[&str]) -> String {
35-
String::new()
33+
words
34+
.iter()
35+
.map(|word| capitalize_first(word))
36+
.fold(String::new(), |acc, item| acc + &item)
3637
}
3738

3839
#[cfg(test)]

exercises/lifetimes/lifetimes1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
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 {
1412
if x.len() > y.len() {
1513
x
1614
} else {

exercises/lifetimes/lifetimes2.rs

Lines changed: 1 addition & 3 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);
2725
}

exercises/lifetimes/lifetimes3.rs

Lines changed: 3 additions & 5 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() {

exercises/macros/macros1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
macro_rules! my_macro {
97
() => {
108
println!("Check out my macro!");
119
};
1210
}
1311

1412
fn main() {
15-
my_macro();
13+
my_macro!();
1614
}

exercises/macros/macros2.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
8-
fn main() {
9-
my_macro!();
10-
}
11-
126
macro_rules! my_macro {
137
() => {
148
println!("Check out my macro!");
159
};
1610
}
11+
12+
fn main() {
13+
my_macro!();
14+
}
15+

exercises/options/options1.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
76

87
// This function returns how much icecream there is left in the fridge.
98
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
@@ -13,7 +12,13 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
1312
// value of 0 The Option output should gracefully handle cases where
1413
// time_of_day > 23.
1514
// TODO: Complete the function body - remember to return an Option!
16-
???
15+
if time_of_day > 23 {
16+
None
17+
} else if time_of_day > 21 {
18+
Some(0)
19+
} else {
20+
Some(5)
21+
}
1722
}
1823

1924
#[cfg(test)]
@@ -34,6 +39,6 @@ mod tests {
3439
// TODO: Fix this test. How do you get at the value contained in the
3540
// Option?
3641
let icecreams = maybe_icecream(12);
37-
assert_eq!(icecreams, 5);
42+
assert_eq!(icecreams, Some(5));
3843
}
3944
}

0 commit comments

Comments
 (0)