Skip to content

Commit 82a67eb

Browse files
yllmisyllmis
authored andcommitted
update
1 parent 875a870 commit 82a67eb

File tree

9 files changed

+42
-39
lines changed

9 files changed

+42
-39
lines changed

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(|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+
words
34+
.iter()
35+
.map(|w| capitalize_first(w))
36+
.collect::<String>()
3637
}
3738

3839
#[cfg(test)]

exercises/iterators/iterators3.rs

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

12-
// I AM NOT DONE
13-
1412
#[derive(Debug, PartialEq, Eq)]
1513
pub enum DivisionError {
1614
NotDivisible(NotDivisibleError),
@@ -26,23 +24,34 @@ pub struct NotDivisibleError {
2624
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
2725
// Otherwise, return a suitable error.
2826
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
29-
todo!();
27+
if b == 0 {
28+
return Err(DivisionError::DivideByZero);
29+
} else if a % b != 0 {
30+
return Err(DivisionError::NotDivisible(NotDivisibleError {
31+
dividend: a,
32+
divisor: b,
33+
}));
34+
} else {
35+
return Ok(a / b);
36+
}
3037
}
3138

3239
// Complete the function and return a value of the correct type so the test
3340
// passes.
3441
// Desired output: Ok([1, 11, 1426, 3])
35-
fn result_with_list() -> () {
42+
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
3643
let numbers = vec![27, 297, 38502, 81];
37-
let division_results = numbers.into_iter().map(|n| divide(n, 27));
44+
let division_results = numbers.into_iter().map(|n| divide(n, 27)).collect();
45+
division_results
3846
}
3947

4048
// Complete the function and return a value of the correct type so the test
4149
// passes.
4250
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
43-
fn list_of_results() -> () {
51+
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
4452
let numbers = vec![27, 297, 38502, 81];
45-
let division_results = numbers.into_iter().map(|n| divide(n, 27));
53+
let division_results = numbers.into_iter().map(|n| divide(n, 27)).collect();
54+
division_results
4655
}
4756

4857
#[cfg(test)]

exercises/iterators/iterators4.rs

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

6-
// I AM NOT DONE
7-
86
pub fn factorial(num: u64) -> u64 {
97
// Complete this function to return the factorial of num
108
// Do not use:
@@ -15,6 +13,7 @@ pub fn factorial(num: u64) -> u64 {
1513
// For an extra challenge, don't use:
1614
// - recursion
1715
// Execute `rustlings hint iterators4` for hints.
16+
(1..=num).product()
1817
}
1918

2019
#[cfg(test)]

exercises/iterators/iterators5.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// hint.
1313

1414
// I AM NOT DONE
15-
1615
use std::collections::HashMap;
1716

1817
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -35,7 +34,7 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
3534
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
3635
// map is a hashmap with String keys and Progress values.
3736
// map = { "variables1": Complete, "from_str": None, ... }
38-
todo!();
37+
map.values().filter(|&&v| v == value).count()
3938
}
4039

4140
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
@@ -54,7 +53,10 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
5453
// collection is a slice of hashmaps.
5554
// collection = [{ "variables1": Complete, "from_str": None, ... },
5655
// { "variables2": Complete, ... }, ... ]
57-
todo!();
56+
collection
57+
.iter()
58+
.map(|map| count_iterator(map, value))
59+
.sum()
5860
}
5961

6062
#[cfg(test)]

exercises/tests/tests1.rs

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

13-
// I AM NOT DONE
14-
1513
#[cfg(test)]
1614
mod tests {
1715
#[test]
1816
fn you_can_assert() {
19-
assert!();
17+
assert!(true);
2018
}
2119
}

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!(false, false);
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(4));
2321
}
2422

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

exercises/tests/tests4.rs

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

8-
// I AM NOT DONE
9-
108
struct Rectangle {
119
width: i32,
12-
height: i32
10+
height: i32,
1311
}
1412

1513
impl Rectangle {
@@ -18,7 +16,7 @@ impl Rectangle {
1816
if width <= 0 || height <= 0 {
1917
panic!("Rectangle width and height cannot be negative!")
2018
}
21-
Rectangle {width, height}
19+
Rectangle { width, height }
2220
}
2321
}
2422

@@ -30,17 +28,19 @@ mod tests {
3028
fn correct_width_and_height() {
3129
// This test should check if the rectangle is the size that we pass into its constructor
3230
let rect = Rectangle::new(10, 20);
33-
assert_eq!(???, 10); // check width
34-
assert_eq!(???, 20); // check height
31+
assert_eq!(rect.width, 10); // check width
32+
assert_eq!(rect.height, 20); // check height
3533
}
3634

3735
#[test]
36+
#[should_panic]
3837
fn negative_width() {
3938
// This test should check if program panics when we try to create rectangle with negative width
4039
let _rect = Rectangle::new(-10, 10);
4140
}
4241

4342
#[test]
43+
#[should_panic]
4444
fn negative_height() {
4545
// This test should check if program panics when we try to create rectangle with negative height
4646
let _rect = Rectangle::new(10, -10);

0 commit comments

Comments
 (0)