小编Edm*_*cho的帖子

为什么编译器阻止我在使用collect()创建的Vec上使用推?

编译如下:

pub fn build_proverb(list: &[&str]) -> String {
    if list.is_empty() {
        return String::new();
    }
    let mut result = (0..list.len() - 1)
        .map(|i| format!("For want of a {} the {} was lost.", list[i], list[i + 1]))
        .collect::<Vec<String>>();
    result.push(format!("And all for the want of a {}.", list[0]));
    result.join("\n")
}
Run Code Online (Sandbox Code Playgroud)

以下不是(请参见Playground):

pub fn build_proverb(list: &[&str]) -> String {
    if list.is_empty() {
        return String::new();
    }
    let mut result = (0..list.len() - 1)
        .map(|i| format!("For want of a {} the {} was lost.", …
Run Code Online (Sandbox Code Playgroud)

collect rust

-2
推荐指数
1
解决办法
123
查看次数

标签 统计

collect ×1

rust ×1