Merge pull request #648 from apogeeoak/iterator2

Moved iterators2.rs errors out of tests.
This commit is contained in:
marisa 2021-04-20 11:24:10 +02:00 committed by GitHub
commit 2b766ef9f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 24 deletions

View file

@ -1,28 +1,41 @@
// iterators2.rs // iterators2.rs
// In this module, you'll learn some of the unique advantages that iterators can offer. // In this exercise, you'll learn some of the unique advantages that iterators
// Step 1. Complete the `capitalize_first` function to pass the first two cases. // can offer. Follow the steps to complete the exercise.
// 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.
// As always, there are hints if you execute `rustlings hint iterators2`! // As always, there are hints if you execute `rustlings hint iterators2`!
// I AM NOT DONE // I AM NOT DONE
// Step 1.
// Complete the `capitalize_first` function.
// "hello" -> "Hello"
pub fn capitalize_first(input: &str) -> String { pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars(); let mut c = input.chars();
match c.next() { match c.next() {
None => String::new(), None => String::new(),
Some(first) => first.collect::<String>() + 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<String> {
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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
// Step 1.
// Tests that verify your `capitalize_first` function implementation
#[test] #[test]
fn test_success() { fn test_success() {
assert_eq!(capitalize_first("hello"), "Hello"); assert_eq!(capitalize_first("hello"), "Hello");
@ -33,18 +46,15 @@ mod tests {
assert_eq!(capitalize_first(""), ""); assert_eq!(capitalize_first(""), "");
} }
// Step 2.
#[test] #[test]
fn test_iterate_string_vec() { fn test_iterate_string_vec() {
let words = vec!["hello", "world"]; let words = vec!["hello", "world"];
let capitalized_words: Vec<String> = // TODO assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]);
assert_eq!(capitalized_words, ["Hello", "World"]);
} }
#[test] #[test]
fn test_iterate_into_string() { fn test_iterate_into_string() {
let words = vec!["hello", " ", "world"]; let words = vec!["hello", " ", "world"];
let capitalized_words = // TODO assert_eq!(capitalize_words_string(&words), "Hello World");
assert_eq!(capitalized_words, "Hello World");
} }
} }

View file

@ -704,21 +704,20 @@ path = "exercises/standard_library_types/iterators2.rs"
mode = "test" mode = "test"
hint = """ hint = """
Step 1 Step 1
You need to call something on `first` before it can be collected The variable `first` is a `char`. It needs to be capitalized and added to the
Currently its type is `char`. Have a look at the methods that are available on that type: 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 https://doc.rust-lang.org/std/primitive.char.html
Step 2 Step 2
First you'll need to turn the Vec into an iterator Create an iterator from the slice. Transform the iterated values by applying
Then you'll need to apply your function unto each item in the vector the `capitalize_first` function. Remember to collect the iterator.
P.s. Don't forget to collect() at the end!
Step 3. Step 3.
This is very similar to the previous test. The only real change is that you will need to This is surprising similar to the previous solution. Collect is very powerful
alter the type that collect is coerced into. For a bonus you could try doing this with a and very general. Rust just needs to know the desired type."""
turbofish"""
[[exercises]] [[exercises]]
name = "iterators3" name = "iterators3"