dyn*_*eed 1 closures iterator rust
我在Rust的这个函数中大写了一个字符串。
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(),
}
}
Run Code Online (Sandbox Code Playgroud)
后来,我用它遍历字符串向量。
let words = vec!["hello", "world"];
let capitalized_words: Vec<String> =
words.iter().map(|word| capitalize_first(word)).collect();
Run Code Online (Sandbox Code Playgroud)
这可以按预期工作,但是我注意到关闭|word| capitalize_first(word)非常没用。所以我试图用这样的capitalize_first直接传递代替它。
let words = vec!["hello", "world"];
let capitalized_words: Vec<String> = words.iter().map(capitalize_first).collect();
Run Code Online (Sandbox Code Playgroud)
但是,这无法通过以下错误消息进行编译。
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) -> _`
Run Code Online (Sandbox Code Playgroud)
我无法理解此错误。为什么闭包有效,但直接传递函数无效。有什么我可以更改的东西,可以让我传递函数引用而不是进行无用的关闭吗?
当您像调用那样遍历一个集合时words.iter(),您正在遍历对元素的引用。向量中的元素属于类型&str,因此对元素的引用也属于类型&&str。
因此map,期望一个函数接受type类型&&str的word参数,这就是推断闭包中的参数的方式。然后当你调用capitalize_first上word,它会自动解除引用&str,由于Deref所有引用正在实施的特点。
但是,即使&&str由于该转换您的函数似乎接受类型的参数,该转换也会在函数外部进行。因此这并不意味着您的函数可以代替期望的函数来传递&&str。
有2个解决方案。您可以将函数更改为更通用的函数,并使其接受任何实现的函数AsRef<str>。然后,它会接受任何可以被解除引用str,其中包括&str,&&str和String,等等。
pub fn capitalize_first<S: AsRef<str>>(input: S) -> String {
let mut c = input.as_ref().chars();
// Don't forget this ^
match c.next() {
None => String::new(),
Some(first) => first.to_uppercase().collect::<String>() + c.as_str(),
}
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以按原样保留函数,并通过调用copied()迭代器来对其进行修复,这实际上将取消对元素的引用。
let capitalized_words: Vec<String> = words.iter().copied().map(capitalize_first).collect();
Run Code Online (Sandbox Code Playgroud)
我建议第一种方法。
| 归档时间: |
|
| 查看次数: |
62 次 |
| 最近记录: |