无法返回函数的字符串类型

时间:2020-07-23 12:58:17

标签: rust

我正在尝试解决一个实际问题,即给出包含单词的输入列表。我必须将它们逐句嵌入。

程序几乎完成,除了我无法弄清楚如何返回String类型。实际上,我必须形成一个向量,以便返回多个语句。现在,在将句子放入Vector之后,问题是如何返回vector元素(尽可能多的列表元素)。

pub fn build_proverb(list: &[&str]) -> String {
    let empty = String::new();
    let mut vecList: Vec<String> = Vec::new();

    // match list.len() {

    // }

    if list.len() < 1 {
        empty
    } else {
        for i in 0..(list.len()) {
            if (i + 1) < list.len() {
                vecList.push(format!(
                    "For want of a {} the {} was lost.\n",
                    list[i],
                    list[i + 1]
                ));
            }
            if i == list.len() - 1 {
                vecList.push(format!("And all for the want of a {}.\n", list[0]));
            }
        }

        vecList // This is where i am getting return type error
    }
}

还有一些我必须对其进行测试的测试用例:

use proverb::build_proverb;

#[test]
fn test_two_pieces() {
    let input = vec!["nail", "shoe"];
    let expected = vec![
        "For want of a nail the shoe was lost.",
        "And all for the want of a nail.",
    ]
    .join("\n");
    assert_eq!(build_proverb(&input), expected);
}

// Notice the change in the last line at three pieces.
#[test]
#[ignore]
fn test_three_pieces() {
    let input = vec!["nail", "shoe", "horse"];
    let expected = vec![
        "For want of a nail the shoe was lost.",
        "For want of a shoe the horse was lost.",
        "And all for the want of a nail.",
    ]
    .join("\n");
    assert_eq!(build_proverb(&input), expected);
}

#[test]
#[ignore]
fn test_one_piece() {
    let input = vec!["nail"];
    let expected = String::from("And all for the want of a nail.");
    assert_eq!(build_proverb(&input), expected);
}

#[test]
#[ignore]
fn test_zero_pieces() {
    let input: Vec<&str> = vec![];
    let expected = String::new();
    assert_eq!(build_proverb(&input), expected);
}

#[test]
#[ignore]
fn test_full() {
    let input = vec![
        "nail", "shoe", "horse", "rider", "message", "battle", "kingdom",
    ];
    let expected = vec![
        "For want of a nail the shoe was lost.",
        "For want of a shoe the horse was lost.",
        "For want of a horse the rider was lost.",
        "For want of a rider the message was lost.",
        "For want of a message the battle was lost.",
        "For want of a battle the kingdom was lost.",
        "And all for the want of a nail.",
    ]
    .join("\n");
    assert_eq!(build_proverb(&input), expected);
}

#[test]
#[ignore]
fn test_three_pieces_modernized() {
    let input = vec!["pin", "gun", "soldier", "battle"];
    let expected = vec![
        "For want of a pin the gun was lost.",
        "For want of a gun the soldier was lost.",
        "For want of a soldier the battle was lost.",
        "And all for the want of a pin.",
    ]
    .join("\n");
    assert_eq!(build_proverb(&input), expected);
}

1 个答案:

答案 0 :(得分:0)

vecList必须移出else块,因此它不会从该范围返回,而是从该函数的范围返回。另外,您可以将其更改为vecList.join("\n"),使其符合函数的返回类型String。

Running on Rust Playground