I've got this function in Rust that capitalizes a string.
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(), } } Later, I use it to iterate over a vector of strings.
let words = vec!["hello", "world"]; let capitalized_words: Vec<String> = words.iter().map(|word| capitalize_first(word)).collect(); This works as expected, but I notice that the closure |word| capitalize_first(word) is pretty useless. So I tried to replace it with passing capitalize_first directly like this.
let words = vec!["hello", "world"]; let capitalized_words: Vec<String> = words.iter().map(capitalize_first).collect(); This, however, fails to compile with the following error message.
10 | pub fn capitalize_first(input: &str) -> String { | ---------------------------------------------- found signature of `for<'r> fn(&'r str) -> _` ... 38 | let capitalized_words: Vec<String> = words.iter().map(capitalize_first).collect(); | ^^^^^^^^^^^^^^^^ expected signature of `fn(&&str) -> _` I'm having trouble understanding this error. Why does the closure work but passing the function directly does not. Is there something I can change that would allow me to pass the function reference instead of making a useless closure?