Skip to content

Commit 242cd3f

Browse files
committedFeb 7, 2025·
30/30
1 parent 7cc0b44 commit 242cd3f

File tree

17 files changed

+64
-80
lines changed

17 files changed

+64
-80
lines changed
 

Diff for: ‎exercises/generics/generics1.rs

+1-3
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
}

Diff for: ‎exercises/generics/generics2.rs

+4-6
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
}

Diff for: ‎exercises/iterators/iterators1.rs

+4-6
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
}

Diff for: ‎exercises/iterators/iterators2.rs

+6-5
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)]

Diff for: ‎exercises/lifetimes/lifetimes1.rs

+1-3
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 {

Diff for: ‎exercises/lifetimes/lifetimes2.rs

+1-3
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
}

Diff for: ‎exercises/lifetimes/lifetimes3.rs

+3-5
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() {

Diff for: ‎exercises/macros/macros1.rs

+1-3
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
}

Diff for: ‎exercises/macros/macros2.rs

+5-6
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+

Diff for: ‎exercises/options/options1.rs

+8-3
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
}

Diff for: ‎exercises/quiz3.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@
1616
//
1717
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
1818

19-
// I AM NOT DONE
19+
use std::fmt::Display;
2020

21-
pub struct ReportCard {
22-
pub grade: f32,
21+
pub struct ReportCard<T> {
22+
pub grade: T,
2323
pub student_name: String,
2424
pub student_age: u8,
2525
}
2626

27-
impl ReportCard {
27+
impl<T: Display> ReportCard<T> {
2828
pub fn print(&self) -> String {
29-
format!("{} ({}) - achieved a grade of {}",
30-
&self.student_name, &self.student_age, &self.grade)
29+
format!(
30+
"{} ({}) - achieved a grade of {}",
31+
&self.student_name, &self.student_age, &self.grade
32+
)
3133
}
3234
}
3335

@@ -50,9 +52,8 @@ mod tests {
5052

5153
#[test]
5254
fn generate_alphabetic_report_card() {
53-
// TODO: Make sure to change the grade here after you finish the exercise.
5455
let report_card = ReportCard {
55-
grade: 2.1,
56+
grade: "A+",
5657
student_name: "Gary Plotter".to_string(),
5758
student_age: 11,
5859
};

Diff for: ‎exercises/smart_pointers/arc1.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@
2121
//
2222
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
2323

24-
// I AM NOT DONE
25-
2624
#![forbid(unused_imports)] // Do not change this, (or the next) line.
2725
use std::sync::Arc;
2826
use std::thread;
2927

3028
fn main() {
3129
let numbers: Vec<_> = (0..100u32).collect();
32-
let shared_numbers = // TODO
30+
let shared_numbers = Arc::new(numbers);
3331
let mut joinhandles = Vec::new();
3432

3533
for offset in 0..8 {
36-
let child_numbers = // TODO
34+
let child_numbers = Arc::clone(&shared_numbers);
3735
joinhandles.push(thread::spawn(move || {
3836
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
3937
println!("Sum of offset {} is {}", offset, sum);

Diff for: ‎exercises/smart_pointers/box1.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
//
1919
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
2020

21-
// I AM NOT DONE
22-
2321
#[derive(PartialEq, Debug)]
2422
pub enum List {
25-
Cons(i32, List),
23+
Cons(i32, Box<List>),
2624
Nil,
2725
}
2826

@@ -35,11 +33,11 @@ fn main() {
3533
}
3634

3735
pub fn create_empty_list() -> List {
38-
todo!()
36+
List::Nil
3937
}
4038

4139
pub fn create_non_empty_list() -> List {
42-
todo!()
40+
List::Cons(0, Box::new(List::Nil))
4341
}
4442

4543
#[cfg(test)]

Diff for: ‎exercises/smart_pointers/cow1.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
//
1313
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
1414

15-
// I AM NOT DONE
16-
1715
use std::borrow::Cow;
1816

1917
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
@@ -48,7 +46,8 @@ mod tests {
4846
let slice = [0, 1, 2];
4947
let mut input = Cow::from(&slice[..]);
5048
match abs_all(&mut input) {
51-
// TODO
49+
Cow::Borrowed(_) => Ok(()),
50+
_ => Err("Expected Borrowed value"),
5251
}
5352
}
5453

@@ -60,7 +59,8 @@ mod tests {
6059
let slice = vec![0, 1, 2];
6160
let mut input = Cow::from(slice);
6261
match abs_all(&mut input) {
63-
// TODO
62+
Cow::Owned(_) => Ok(()),
63+
_ => Err("Expected owned value"),
6464
}
6565
}
6666

@@ -72,7 +72,8 @@ mod tests {
7272
let slice = vec![-1, 0, 1];
7373
let mut input = Cow::from(slice);
7474
match abs_all(&mut input) {
75-
// TODO
75+
Cow::Owned(_) => Ok(()),
76+
_ => Err("Clone occurred"),
7677
}
7778
}
7879
}

Diff for: ‎exercises/smart_pointers/rc1.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
//
1111
// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
1212

13-
// I AM NOT DONE
14-
1513
use std::rc::Rc;
1614

1715
#[derive(Debug)]
@@ -59,18 +57,16 @@ fn main() {
5957
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
6058
jupiter.details();
6159

62-
// TODO
63-
let saturn = Planet::Saturn(Rc::new(Sun {}));
60+
let saturn = Planet::Saturn(Rc::clone(&sun));
6461
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
6562
saturn.details();
6663

67-
// TODO
68-
let uranus = Planet::Uranus(Rc::new(Sun {}));
64+
let uranus = Planet::Uranus(Rc::clone(&sun));
6965
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
7066
uranus.details();
7167

7268
// TODO
73-
let neptune = Planet::Neptune(Rc::new(Sun {}));
69+
let neptune = Planet::Neptune(Rc::clone(&sun));
7470
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
7571
neptune.details();
7672

@@ -91,13 +87,13 @@ fn main() {
9187
drop(mars);
9288
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
9389

94-
// TODO
90+
drop(earth);
9591
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
9692

97-
// TODO
93+
drop(venus);
9894
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
9995

100-
// TODO
96+
drop(mercury);
10197
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
10298

10399
assert_eq!(Rc::strong_count(&sun), 1);

Diff for: ‎exercises/traits/traits3.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
99
// hint.
1010

11-
// I AM NOT DONE
12-
1311
pub trait Licensed {
14-
fn licensing_info(&self) -> String;
12+
fn licensing_info(&self) -> String {
13+
String::from("Some information")
14+
}
1515
}
1616

1717
struct SomeSoftware {

Diff for: ‎exercises/traits/traits4.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
1110

1211
pub trait Licensed {
1312
fn licensing_info(&self) -> String {
@@ -23,7 +22,7 @@ impl Licensed for SomeSoftware {}
2322
impl Licensed for OtherSoftware {}
2423

2524
// YOU MAY ONLY CHANGE THE NEXT LINE
26-
fn compare_license_types(software: ??, software_two: ??) -> bool {
25+
fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool {
2726
software.licensing_info() == software_two.licensing_info()
2827
}
2928

0 commit comments

Comments
 (0)
Please sign in to comment.