Skip to content

Commit baf4ba1

Browse files
committed
fix(iterators2): Moved errors out of tests.
Closes #359
1 parent ab57c26 commit baf4ba1

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed
+24-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
// iterators2.rs
2-
// In this module, you'll learn some of the unique advantages that iterators can offer.
3-
// Step 1. Complete the `capitalize_first` function to pass the first two cases.
4-
// Step 2. Apply the `capitalize_first` function to a vector of strings.
5-
// Ensure that it returns a vector of strings as well.
6-
// Step 3. Apply the `capitalize_first` function again to a list.
7-
// Try to ensure it returns a single string.
2+
// In this exercise, you'll learn some of the unique advantages that iterators
3+
// can offer. Follow the steps to complete the exercise.
84
// As always, there are hints if you execute `rustlings hint iterators2`!
95

106
// I AM NOT DONE
117

8+
// Step 1.
9+
// Complete the `capitalize_first` function.
10+
// "hello" -> "Hello"
1211
pub fn capitalize_first(input: &str) -> String {
1312
let mut c = input.chars();
1413
match c.next() {
1514
None => String::new(),
16-
Some(first) => first.collect::<String>() + c.as_str(),
15+
Some(first) => ???,
1716
}
1817
}
1918

19+
// Step 2.
20+
// Apply the `capitalize_first` function to a slice of string slices.
21+
// Return a vector of strings.
22+
// ["hello", "world"] -> ["Hello", "World"]
23+
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
24+
vec![]
25+
}
26+
27+
// Step 3.
28+
// Apply the `capitalize_first` function again to a slice of string slices.
29+
// Return a single string.
30+
// ["hello", " ", "world"] -> "Hello World"
31+
pub fn capitalize_words_string(words: &[&str]) -> String {
32+
String::new()
33+
}
34+
2035
#[cfg(test)]
2136
mod tests {
2237
use super::*;
2338

24-
// Step 1.
25-
// Tests that verify your `capitalize_first` function implementation
2639
#[test]
2740
fn test_success() {
2841
assert_eq!(capitalize_first("hello"), "Hello");
@@ -33,18 +46,15 @@ mod tests {
3346
assert_eq!(capitalize_first(""), "");
3447
}
3548

36-
// Step 2.
3749
#[test]
3850
fn test_iterate_string_vec() {
3951
let words = vec!["hello", "world"];
40-
let capitalized_words: Vec<String> = // TODO
41-
assert_eq!(capitalized_words, ["Hello", "World"]);
52+
assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]);
4253
}
4354

4455
#[test]
4556
fn test_iterate_into_string() {
4657
let words = vec!["hello", " ", "world"];
47-
let capitalized_words = // TODO
48-
assert_eq!(capitalized_words, "Hello World");
58+
assert_eq!(capitalize_words_string(&words), "Hello World");
4959
}
5060
}

info.toml

+9-10
Original file line numberDiff line numberDiff line change
@@ -704,21 +704,20 @@ path = "exercises/standard_library_types/iterators2.rs"
704704
mode = "test"
705705
hint = """
706706
Step 1
707-
You need to call something on `first` before it can be collected
708-
Currently its type is `char`. Have a look at the methods that are available on that type:
707+
The variable `first` is a `char`. It needs to be capitalized and added to the
708+
remaining characters in `c` in order to return the correct `String`.
709+
The remaining characters in `c` can be viewed as a string slice using the
710+
`as_str` method.
711+
The documentation for `char` contains many useful methods.
709712
https://doc.rust-lang.org/std/primitive.char.html
710713
711-
712714
Step 2
713-
First you'll need to turn the Vec into an iterator
714-
Then you'll need to apply your function unto each item in the vector
715-
P.s. Don't forget to collect() at the end!
716-
715+
Create an iterator from the slice. Transform the iterated values by applying
716+
the `capitalize_first` function. Remember to collect the iterator.
717717
718718
Step 3.
719-
This is very similar to the previous test. The only real change is that you will need to
720-
alter the type that collect is coerced into. For a bonus you could try doing this with a
721-
turbofish"""
719+
This is surprising similar to the previous solution. Collect is very powerful
720+
and very general. Rust just needs to know the desired type."""
722721

723722
[[exercises]]
724723
name = "iterators3"

0 commit comments

Comments
 (0)