Skip to content

Commit 1b636c5

Browse files
committed
do exec
1 parent d660ef9 commit 1b636c5

File tree

7 files changed

+32
-26
lines changed

7 files changed

+32
-26
lines changed

exercises/iterators/iterators1.rs

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

12-
// I AM NOT DONE
1312

1413
fn main() {
1514
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
1615

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

1918
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
20-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
19+
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
2120
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
22-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
21+
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
2322
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
24-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
23+
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
2524
}

exercises/iterators/iterators2.rs

Lines changed: 3 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,15 @@ 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(|w|capitalize_first(w)).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+
capitalize_words_vector(words).iter().fold(String::new(), |mut acc, s| {acc.push_str(s); acc})
3634
}
3735

3836
#[cfg(test)]

exercises/quiz3.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@
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;
20+
pub enum Grade {
21+
Number(f32),
22+
Alphabet(&'static str)
23+
}
2024

2125
pub struct ReportCard {
22-
pub grade: f32,
26+
pub grade: Grade,
2327
pub student_name: String,
2428
pub student_age: u8,
2529
}
2630

31+
impl fmt::Display for Grade {
32+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33+
match self{
34+
Grade::Alphabet(a) => write!(f, "{}", a),
35+
Grade::Number(a) => write!(f, "{}", a),
36+
}
37+
}
38+
}
39+
2740
impl ReportCard {
2841
pub fn print(&self) -> String {
2942
format!("{} ({}) - achieved a grade of {}",
@@ -38,7 +51,7 @@ mod tests {
3851
#[test]
3952
fn generate_numeric_report_card() {
4053
let report_card = ReportCard {
41-
grade: 2.1,
54+
grade: Grade::Number(2.1),
4255
student_name: "Tom Wriggle".to_string(),
4356
student_age: 12,
4457
};
@@ -52,7 +65,7 @@ mod tests {
5265
fn generate_alphabetic_report_card() {
5366
// TODO: Make sure to change the grade here after you finish the exercise.
5467
let report_card = ReportCard {
55-
grade: 2.1,
68+
grade: Grade::Alphabet("A+"),
5669
student_name: "Gary Plotter".to_string(),
5770
student_age: 11,
5871
};

exercises/tests/tests1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
1111
// hint.
1212

13-
// I AM NOT DONE
1413

1514
#[cfg(test)]
1615
mod tests {
1716
#[test]
1817
fn you_can_assert() {
19-
assert!();
18+
assert!(true);
2019
}
2120
}

exercises/tests/tests2.rs

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

9-
// I AM NOT DONE
10-
119
#[cfg(test)]
1210
mod tests {
1311
#[test]
1412
fn you_can_assert_eq() {
15-
assert_eq!();
13+
assert_eq!(1,1);
1614
}
1715
}

exercises/tests/tests3.rs

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

10-
// I AM NOT DONE
11-
1210
pub fn is_even(num: i32) -> bool {
1311
num % 2 == 0
1412
}
@@ -19,11 +17,11 @@ mod tests {
1917

2018
#[test]
2119
fn is_true_when_even() {
22-
assert!();
20+
assert!(is_even(2));
2321
}
2422

2523
#[test]
2624
fn is_false_when_odd() {
27-
assert!();
25+
assert!(!is_even(3));
2826
}
2927
}

exercises/tests/tests4.rs

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

8-
// I AM NOT DONE
98

109
struct Rectangle {
1110
width: i32,
@@ -30,17 +29,19 @@ mod tests {
3029
fn correct_width_and_height() {
3130
// This test should check if the rectangle is the size that we pass into its constructor
3231
let rect = Rectangle::new(10, 20);
33-
assert_eq!(???, 10); // check width
34-
assert_eq!(???, 20); // check height
32+
assert_eq!(rect.width, 10); // check width
33+
assert_eq!(rect.height, 20); // check height
3534
}
3635

3736
#[test]
37+
#[should_panic]
3838
fn negative_width() {
3939
// This test should check if program panics when we try to create rectangle with negative width
4040
let _rect = Rectangle::new(-10, 10);
4141
}
4242

4343
#[test]
44+
#[should_panic]
4445
fn negative_height() {
4546
// This test should check if program panics when we try to create rectangle with negative height
4647
let _rect = Rectangle::new(10, -10);

0 commit comments

Comments
 (0)