2021-02-12 02:24:32 +00:00
|
|
|
// In this exercise, you'll learn some of the unique advantages that iterators
|
2024-06-28 01:48:21 +01:00
|
|
|
// can offer.
|
2019-07-02 12:21:58 +01:00
|
|
|
|
2024-06-28 01:48:21 +01:00
|
|
|
// TODO: Complete the `capitalize_first` function.
|
2021-02-12 02:24:32 +00:00
|
|
|
// "hello" -> "Hello"
|
2024-05-22 14:04:12 +01:00
|
|
|
fn capitalize_first(input: &str) -> String {
|
2024-06-28 01:48:21 +01:00
|
|
|
let mut chars = input.chars();
|
|
|
|
match chars.next() {
|
2019-07-02 12:21:58 +01:00
|
|
|
None => String::new(),
|
2024-06-28 01:48:21 +01:00
|
|
|
Some(first) => todo!(),
|
2019-07-02 12:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-28 01:48:21 +01:00
|
|
|
// TODO: Apply the `capitalize_first` function to a slice of string slices.
|
2021-02-12 02:24:32 +00:00
|
|
|
// Return a vector of strings.
|
|
|
|
// ["hello", "world"] -> ["Hello", "World"]
|
2024-05-22 14:04:12 +01:00
|
|
|
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
2024-06-28 01:48:21 +01:00
|
|
|
// ???
|
2021-02-12 02:24:32 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 01:48:21 +01:00
|
|
|
// TODO: Apply the `capitalize_first` function again to a slice of string
|
|
|
|
// slices. Return a single string.
|
2021-02-12 02:24:32 +00:00
|
|
|
// ["hello", " ", "world"] -> "Hello World"
|
2024-05-22 14:04:12 +01:00
|
|
|
fn capitalize_words_string(words: &[&str]) -> String {
|
2024-06-28 01:48:21 +01:00
|
|
|
// ???
|
2021-02-12 02:24:32 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:46:21 +01:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2019-07-02 12:21:58 +01:00
|
|
|
#[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"];
|
2021-02-12 02:24:32 +00:00
|
|
|
assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]);
|
2019-07-02 12:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterate_into_string() {
|
|
|
|
let words = vec!["hello", " ", "world"];
|
2021-02-12 02:24:32 +00:00
|
|
|
assert_eq!(capitalize_words_string(&words), "Hello World");
|
2019-07-02 12:21:58 +01:00
|
|
|
}
|
|
|
|
}
|