Skip to content

Commit 2f6b6f5

Browse files
committed
update
1 parent 2dbab0c commit 2f6b6f5

File tree

24 files changed

+86
-68
lines changed

24 files changed

+86
-68
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
77
// hint.
88

9-
// I AM NOT DONE
109

1110
// Step 1.
1211
// Complete the `capitalize_first` function.
@@ -15,7 +14,7 @@ pub fn capitalize_first(input: &str) -> String {
1514
let mut c = input.chars();
1615
match c.next() {
1716
None => String::new(),
18-
Some(first) => ???,
17+
Some(first) => first.to_uppercase().collect::<String>() + c.as_str(),
1918
}
2019
}
2120

@@ -24,15 +23,15 @@ pub fn capitalize_first(input: &str) -> String {
2423
// Return a vector of strings.
2524
// ["hello", "world"] -> ["Hello", "World"]
2625
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
27-
vec![]
26+
words.iter().map(|&word| capitalize_first(word)).collect::<Vec<String>>()
2827
}
2928

3029
// Step 3.
3130
// Apply the `capitalize_first` function again to a slice of string slices.
3231
// Return a single string.
3332
// ["hello", " ", "world"] -> "Hello World"
3433
pub fn capitalize_words_string(words: &[&str]) -> String {
35-
String::new()
34+
words.iter().map(|&word| capitalize_first(word)).collect::<Vec<String>>().join("")
3635
}
3736

3837
#[cfg(test)]

exercises/iterators/iterators3.rs

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

12-
// I AM NOT DONE
1312

1413
#[derive(Debug, PartialEq, Eq)]
1514
pub enum DivisionError {
@@ -26,23 +25,33 @@ pub struct NotDivisibleError {
2625
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
2726
// Otherwise, return a suitable error.
2827
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
29-
todo!();
28+
if b == 0 {
29+
Err(DivisionError::DivideByZero)
30+
}
31+
else if a%b != 0 {
32+
Err(DivisionError::NotDivisible(NotDivisibleError{dividend: a, divisor: b}))
33+
}
34+
else{
35+
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];
3744
let division_results = numbers.into_iter().map(|n| divide(n, 27));
45+
division_results.collect()
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];
4553
let division_results = numbers.into_iter().map(|n| divide(n, 27));
54+
division_results.collect()
4655
}
4756

4857
#[cfg(test)]

exercises/iterators/iterators4.rs

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

6-
// I AM NOT DONE
76

87
pub fn factorial(num: u64) -> u64 {
98
// Complete this function to return the factorial of num
@@ -15,6 +14,7 @@ pub fn factorial(num: u64) -> u64 {
1514
// For an extra challenge, don't use:
1615
// - recursion
1716
// Execute `rustlings hint iterators4` for hints.
17+
(1..=num).fold(1, |acc, x| acc * x)
1818
}
1919

2020
#[cfg(test)]

exercises/iterators/iterators5.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
1212
// hint.
1313

14-
// I AM NOT DONE
1514

1615
use std::collections::HashMap;
1716

@@ -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(|&val| *val == value).count()
3938
}
4039

4140
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
@@ -54,7 +53,7 @@ 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.iter().map(|map| map.values().filter(|&val| *val == value).count()).sum()
5857
}
5958

6059
#[cfg(test)]

exercises/lifetimes/lifetimes1.rs

Lines changed: 1 addition & 2 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
1211

13-
fn longest(x: &str, y: &str) -> &str {
12+
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1413
if x.len() > y.len() {
1514
x
1615
} else {

exercises/lifetimes/lifetimes2.rs

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

9-
// I AM NOT DONE
109

1110
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1211
if x.len() > y.len() {
@@ -19,9 +18,7 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1918
fn main() {
2019
let string1 = String::from("long string is long");
2120
let result;
22-
{
23-
let string2 = String::from("xyz");
24-
result = longest(string1.as_str(), string2.as_str());
25-
}
21+
let string2 = String::from("xyz");
22+
result = longest(string1.as_str(), string2.as_str());
2623
println!("The longest string is '{}'", result);
2724
}

exercises/lifetimes/lifetimes3.rs

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

8-
// I AM NOT DONE
98

10-
struct Book {
11-
author: &str,
12-
title: &str,
9+
struct Book<'a> {
10+
author: &'a str,
11+
title: &'a str,
1312
}
1413

1514
fn main() {

exercises/smart_pointers/arc1.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@
2121
//
2222
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
2323

24-
// I AM NOT DONE
2524

2625
#![forbid(unused_imports)] // Do not change this, (or the next) line.
2726
use std::sync::Arc;
2827
use std::thread;
2928

3029
fn main() {
3130
let numbers: Vec<_> = (0..100u32).collect();
32-
let shared_numbers = // TODO
31+
let shared_numbers = Arc::new(numbers);
3332
let mut joinhandles = Vec::new();
3433

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

exercises/smart_pointers/box1.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
//
1919
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
2020

21-
// I AM NOT DONE
2221

2322
#[derive(PartialEq, Debug)]
2423
pub enum List {
25-
Cons(i32, List),
24+
Cons(i32, Box<List>),
2625
Nil,
2726
}
2827

@@ -35,11 +34,11 @@ fn main() {
3534
}
3635

3736
pub fn create_empty_list() -> List {
38-
todo!()
37+
List::Nil
3938
}
4039

4140
pub fn create_non_empty_list() -> List {
42-
todo!()
41+
List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))))
4342
}
4443

4544
#[cfg(test)]

0 commit comments

Comments
 (0)