2019-07-02 06:21:58 -05:00
|
|
|
// iterators2.rs
|
2020-11-07 06:54:14 -06:00
|
|
|
// In this module, you'll learn some of the unique advantages that iterators can offer.
|
2020-04-29 21:11:54 -05:00
|
|
|
// 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.
|
2019-11-11 09:51:38 -06:00
|
|
|
// As always, there are hints if you execute `rustlings hint iterators2`!
|
2019-07-02 06:21:58 -05:00
|
|
|
|
2019-11-11 06:38:24 -06:00
|
|
|
// I AM NOT DONE
|
|
|
|
|
2019-07-02 06:21:58 -05:00
|
|
|
pub fn capitalize_first(input: &str) -> String {
|
|
|
|
let mut c = input.chars();
|
|
|
|
match c.next() {
|
|
|
|
None => String::new(),
|
2019-08-09 09:28:35 -05:00
|
|
|
Some(first) => first.collect::<String>() + c.as_str(),
|
2019-07-02 06:21:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
assert_eq!(capitalize_first(""), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2.
|
|
|
|
#[test]
|
|
|
|
fn test_iterate_string_vec() {
|
|
|
|
let words = vec!["hello", "world"];
|
|
|
|
let capitalized_words: Vec<String> = // TODO
|
|
|
|
assert_eq!(capitalized_words, ["Hello", "World"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterate_into_string() {
|
|
|
|
let words = vec!["hello", " ", "world"];
|
|
|
|
let capitalized_words = // TODO
|
|
|
|
assert_eq!(capitalized_words, "Hello World");
|
|
|
|
}
|
|
|
|
}
|