【发布时间】:2016-01-27 06:41:24
【问题描述】:
我可以写以下两种方式,第二种是受What is the idiomatic way to create a collection of references to methods that take self?启发的:
channels.iter().flat_map(|c|c.to_uppercase()).collect(),
channels.clone().into_iter().flat_map(char::to_uppercase).collect(),
第二行必须克隆集合,因为char::to_uppercase 不接受引用作为它的参数,.iter() 提供引用,.into_iter() 移动集合。
有没有不需要克隆集合或创建闭包的方法来做到这一点?我不讨厌闭包,我保证,而且我知道它们只是在 LLVM 中变成(通常是内联)函数调用,但我喜欢在第二行中引用函数的简洁性,并且更愿意使用它如果它可以在没有克隆的情况下完成。
【问题讨论】:
标签: iterator rust move-semantics