在做rustlings 时standard_library_types/iterators2.rs,我开始想知道如何std::iter::Iterator::map调用它的参数闭包/函数。更具体地说,假设我有一个功能
// "hello" -> "Hello"
pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars();
match c.next() {
None => String::new(),
Some(first) => String::from(first.to_ascii_uppercase()) + c.as_str(),
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想用它
// 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> {
words.into_iter().map(capitalize_first).collect()
}
Run Code Online (Sandbox Code Playgroud)
哪个不编译
error[E0631]: type mismatch in function arguments
--> exercises/standard_library_types/iterators2.rs:24:27
|
11 …Run Code Online (Sandbox Code Playgroud) iterator functional-programming dereference rust trait-objects