mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-21 13:32:23 -05:00
iterators2 solution
This commit is contained in:
parent
4f71f74b44
commit
eddbb97934
3 changed files with 72 additions and 17 deletions
|
@ -1,31 +1,28 @@
|
|||
// In this exercise, you'll learn some of the unique advantages that iterators
|
||||
// can offer. Follow the steps to complete the exercise.
|
||||
// can offer.
|
||||
|
||||
// Step 1.
|
||||
// Complete the `capitalize_first` function.
|
||||
// TODO: Complete the `capitalize_first` function.
|
||||
// "hello" -> "Hello"
|
||||
fn capitalize_first(input: &str) -> String {
|
||||
let mut c = input.chars();
|
||||
match c.next() {
|
||||
let mut chars = input.chars();
|
||||
match chars.next() {
|
||||
None => String::new(),
|
||||
Some(first) => ???,
|
||||
Some(first) => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2.
|
||||
// Apply the `capitalize_first` function to a slice of string slices.
|
||||
// TODO: Apply the `capitalize_first` function to a slice of string slices.
|
||||
// Return a vector of strings.
|
||||
// ["hello", "world"] -> ["Hello", "World"]
|
||||
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.
|
||||
// TODO: Apply the `capitalize_first` function again to a slice of string
|
||||
// slices. Return a single string.
|
||||
// ["hello", " ", "world"] -> "Hello World"
|
||||
fn capitalize_words_string(words: &[&str]) -> String {
|
||||
String::new()
|
||||
// ???
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -894,7 +894,7 @@ https://doc.rust-lang.org/std/iter/trait.Iterator.html"""
|
|||
name = "iterators2"
|
||||
dir = "18_iterators"
|
||||
hint = """
|
||||
Step 1:
|
||||
`capitalize_first`:
|
||||
|
||||
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`.
|
||||
|
@ -905,12 +905,15 @@ The remaining characters in `c` can be viewed as a string slice using the
|
|||
The documentation for `char` contains many useful methods.
|
||||
https://doc.rust-lang.org/std/primitive.char.html
|
||||
|
||||
Step 2:
|
||||
Use `char::to_uppercase`. It returns an iterator that can be converted to a
|
||||
`String`.
|
||||
|
||||
`capitalize_words_vector`:
|
||||
|
||||
Create an iterator from the slice. Transform the iterated values by applying
|
||||
the `capitalize_first` function. Remember to `collect` the iterator.
|
||||
|
||||
Step 3:
|
||||
`capitalize_words_string`:
|
||||
|
||||
This is surprisingly similar to the previous solution. `collect` is very
|
||||
powerful and very general. Rust just needs to know the desired type."""
|
||||
|
|
|
@ -1 +1,56 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
// In this exercise, you'll learn some of the unique advantages that iterators
|
||||
// can offer.
|
||||
|
||||
// "hello" -> "Hello"
|
||||
fn capitalize_first(input: &str) -> String {
|
||||
let mut chars = input.chars();
|
||||
match chars.next() {
|
||||
None => String::new(),
|
||||
Some(first) => first.to_uppercase().to_string() + chars.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the `capitalize_first` function to a slice of string slices.
|
||||
// Return a vector of strings.
|
||||
// ["hello", "world"] -> ["Hello", "World"]
|
||||
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||
words.iter().map(|word| capitalize_first(word)).collect()
|
||||
}
|
||||
|
||||
// Apply the `capitalize_first` function again to a slice of string
|
||||
// slices. Return a single string.
|
||||
// ["hello", " ", "world"] -> "Hello World"
|
||||
fn capitalize_words_string(words: &[&str]) -> String {
|
||||
words.iter().map(|word| capitalize_first(word)).collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_success() {
|
||||
assert_eq!(capitalize_first("hello"), "Hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty() {
|
||||
assert_eq!(capitalize_first(""), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterate_string_vec() {
|
||||
let words = vec!["hello", "world"];
|
||||
assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterate_into_string() {
|
||||
let words = vec!["hello", " ", "world"];
|
||||
assert_eq!(capitalize_words_string(&words), "Hello World");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue