热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

使用iter.map,为什么闭包有效,但直接传递函数却无效?

如何解决《使用iter.map,为什么闭包有效,但直接传递函数却无效?》经验,为你挑选了1个好方法。

我在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类型&&strword参数,这就是推断闭包中的参数的方式。然后当你调用capitalize_firstword,它会自动解除引用&str,由于Deref所有引用正在实施的特点。

但是,即使&&str由于该转换您的函数似乎接受类型的参数,该转换也会在函数外部进行。因此这并不意味着您的函数可以代替期望的函数来传递&&str

有2个解决方案。您可以将函数更改为更通用的函数,并使其接受任何实现的函数AsRef。然后,它会接受任何可以被解除引用str,其中包括&str&&strString,等等。

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();

我建议第一种方法。


推荐阅读
author-avatar
小白兔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有