In the Rustlings tutorial "iterators2", we have a capitalize_first
function like this:
pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars();
match c.next() {
None => String::new(),
Some(first) => first.to_uppercase().collect::<String>() + c.as_str(),
}
}
And we're asked to map it over a vector, which can be accomplished by using a closure:
let words = vec!["hello", "world"];
let capitalized_words: Vec<String> = words.iter().map(|w| capitalize_first(w)).collect();
assert_eq!(capitalized_words, ["Hello", "World"]);
This works, but I also tried the code:
words.iter().map(capitalize_first).collect();
Which does not compile, and gives an error message I don't really understand: "expected signature of fn(&&str) -> _
". In other programming languages I am familiar with, these two pieces of code would be identical. Why am I not allowed to pass the function name directly as an argument?