作者:小白兔 | 来源:互联网 | 2022-10-09 23:50
我在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::() + c.as_str(),
}
}
后来,我用它遍历字符串向量。
let words = vec!["hello", "world"];
let capitalized_words: Vec =
words.iter().map(|word| capitalize_first(word)).collect();
这可以按预期工作,但是我注意到关闭|word| capitalize_first(word)
非常没用。所以我试图用这样的capitalize_first
直接传递代替它。
let words = vec!["hello", "world"];
let capitalized_words: Vec = words.iter().map(capitalize_first).collect();
但是,这无法通过以下错误消息进行编译。
10 | pub fn capitalize_first(input: &str) -> String {
| ---------------------------------------------- found signature of `for<'r> fn(&'r str) -> _`
...
38 | let capitalized_words: Vec = words.iter().map(capitalize_first).collect();
| ^^^^^^^^^^^^^^^^ expected signature of `fn(&&str) -> _`
我无法理解此错误。为什么闭包有效,但直接传递函数无效。有什么我可以更改的东西,可以让我传递函数引用而不是进行无用的关闭吗?
1> Benjamin Lin..:
当您像调用那样遍历一个集合时words.iter()
,您正在遍历对元素的引用。向量中的元素属于类型&str
,因此对元素的引用也属于类型&&str
。
因此map
,期望一个函数接受type类型&&str
的word
参数,这就是推断闭包中的参数的方式。然后当你调用capitalize_first
上word
,它会自动解除引用&str
,由于Deref
所有引用正在实施的特点。
但是,即使&&str
由于该转换您的函数似乎接受类型的参数,该转换也会在函数外部进行。因此这并不意味着您的函数可以代替期望的函数来传递&&str
。
有2个解决方案。您可以将函数更改为更通用的函数,并使其接受任何实现的函数AsRef
。然后,它会接受任何可以被解除引用str
,其中包括&str
,&&str
和String
,等等。
pub fn capitalize_first>(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::() + c.as_str(),
}
}
或者,您可以按原样保留函数,并通过调用copied()
迭代器来对其进行修复,这实际上将取消对元素的引用。
let capitalized_words: Vec = words.iter().copied().map(capitalize_first).collect();
我建议第一种方法。