From baf4ba175ba6eb92989e3dd54ecbec4bedc9a863 Mon Sep 17 00:00:00 2001 From: apogeeoak <59737221+apogeeoak@users.noreply.github.com> Date: Thu, 11 Feb 2021 21:24:32 -0500 Subject: [PATCH] fix(iterators2): Moved errors out of tests. Closes #359 --- .../standard_library_types/iterators2.rs | 38 ++++++++++++------- info.toml | 19 +++++----- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/exercises/standard_library_types/iterators2.rs b/exercises/standard_library_types/iterators2.rs index 84d14ae..87b4eaa 100644 --- a/exercises/standard_library_types/iterators2.rs +++ b/exercises/standard_library_types/iterators2.rs @@ -1,28 +1,41 @@ // iterators2.rs -// In this module, you'll learn some of the unique advantages that iterators can offer. -// Step 1. Complete the `capitalize_first` function to pass the first two cases. -// Step 2. Apply the `capitalize_first` function to a vector of strings. -// Ensure that it returns a vector of strings as well. -// Step 3. Apply the `capitalize_first` function again to a list. -// Try to ensure it returns a single string. +// In this exercise, you'll learn some of the unique advantages that iterators +// can offer. Follow the steps to complete the exercise. // As always, there are hints if you execute `rustlings hint iterators2`! // I AM NOT DONE +// Step 1. +// Complete the `capitalize_first` function. +// "hello" -> "Hello" pub fn capitalize_first(input: &str) -> String { let mut c = input.chars(); match c.next() { None => String::new(), - Some(first) => first.collect::() + c.as_str(), + Some(first) => ???, } } +// Step 2. +// Apply the `capitalize_first` function to a slice of string slices. +// Return a vector of strings. +// ["hello", "world"] -> ["Hello", "World"] +pub fn capitalize_words_vector(words: &[&str]) -> Vec { + vec![] +} + +// Step 3. +// Apply the `capitalize_first` function again to a slice of string slices. +// Return a single string. +// ["hello", " ", "world"] -> "Hello World" +pub fn capitalize_words_string(words: &[&str]) -> String { + String::new() +} + #[cfg(test)] mod tests { use super::*; - // Step 1. - // Tests that verify your `capitalize_first` function implementation #[test] fn test_success() { assert_eq!(capitalize_first("hello"), "Hello"); @@ -33,18 +46,15 @@ mod tests { assert_eq!(capitalize_first(""), ""); } - // Step 2. #[test] fn test_iterate_string_vec() { let words = vec!["hello", "world"]; - let capitalized_words: Vec = // TODO - assert_eq!(capitalized_words, ["Hello", "World"]); + assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]); } #[test] fn test_iterate_into_string() { let words = vec!["hello", " ", "world"]; - let capitalized_words = // TODO - assert_eq!(capitalized_words, "Hello World"); + assert_eq!(capitalize_words_string(&words), "Hello World"); } } diff --git a/info.toml b/info.toml index f0fed93..5570b01 100644 --- a/info.toml +++ b/info.toml @@ -704,21 +704,20 @@ path = "exercises/standard_library_types/iterators2.rs" mode = "test" hint = """ Step 1 -You need to call something on `first` before it can be collected -Currently its type is `char`. Have a look at the methods that are available on that type: +The variable `first` is a `char`. It needs to be capitalized and added to the +remaining characters in `c` in order to return the correct `String`. +The remaining characters in `c` can be viewed as a string slice using the +`as_str` method. +The documentation for `char` contains many useful methods. https://doc.rust-lang.org/std/primitive.char.html - Step 2 -First you'll need to turn the Vec into an iterator -Then you'll need to apply your function unto each item in the vector -P.s. Don't forget to collect() at the end! - +Create an iterator from the slice. Transform the iterated values by applying +the `capitalize_first` function. Remember to collect the iterator. Step 3. -This is very similar to the previous test. The only real change is that you will need to -alter the type that collect is coerced into. For a bonus you could try doing this with a -turbofish""" +This is surprising similar to the previous solution. Collect is very powerful +and very general. Rust just needs to know the desired type.""" [[exercises]] name = "iterators3"