mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
iterators2 solution
This commit is contained in:
parent
4f71f74b44
commit
eddbb97934
|
@ -1,31 +1,28 @@
|
||||||
// In this exercise, you'll learn some of the unique advantages that iterators
|
// 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.
|
// TODO: Complete the `capitalize_first` function.
|
||||||
// Complete the `capitalize_first` function.
|
|
||||||
// "hello" -> "Hello"
|
// "hello" -> "Hello"
|
||||||
fn capitalize_first(input: &str) -> String {
|
fn capitalize_first(input: &str) -> String {
|
||||||
let mut c = input.chars();
|
let mut chars = input.chars();
|
||||||
match c.next() {
|
match chars.next() {
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
Some(first) => ???,
|
Some(first) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2.
|
// TODO: Apply the `capitalize_first` function to a slice of string slices.
|
||||||
// Apply the `capitalize_first` function to a slice of string slices.
|
|
||||||
// Return a vector of strings.
|
// Return a vector of strings.
|
||||||
// ["hello", "world"] -> ["Hello", "World"]
|
// ["hello", "world"] -> ["Hello", "World"]
|
||||||
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
vec![]
|
// ???
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3.
|
// TODO: Apply the `capitalize_first` function again to a slice of string
|
||||||
// Apply the `capitalize_first` function again to a slice of string slices.
|
// slices. Return a single string.
|
||||||
// Return a single string.
|
|
||||||
// ["hello", " ", "world"] -> "Hello World"
|
// ["hello", " ", "world"] -> "Hello World"
|
||||||
fn capitalize_words_string(words: &[&str]) -> String {
|
fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
String::new()
|
// ???
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -894,7 +894,7 @@ https://doc.rust-lang.org/std/iter/trait.Iterator.html"""
|
||||||
name = "iterators2"
|
name = "iterators2"
|
||||||
dir = "18_iterators"
|
dir = "18_iterators"
|
||||||
hint = """
|
hint = """
|
||||||
Step 1:
|
`capitalize_first`:
|
||||||
|
|
||||||
The variable `first` is a `char`. It needs to be capitalized and added to the
|
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`.
|
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.
|
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:
|
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
|
Create an iterator from the slice. Transform the iterated values by applying
|
||||||
the `capitalize_first` function. Remember to `collect` the iterator.
|
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
|
This is surprisingly similar to the previous solution. `collect` is very
|
||||||
powerful and very general. Rust just needs to know the desired type."""
|
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