18: Add solution for Rust track exercise "Proverb".

[?]
Aaw9nJhsNmfzFih9mKyNw9mV8CgERXJkRa1kK1Kx3LQH
Aug 23, 2021, 11:10 AM
QA5RWGHZQHQHGJMWJX6ADDGSW4Q6KVVUH3M4HU7MXUR6SIVYJX2AC

Dependencies

  • [2] QK6XE5XF 13: Add Rust track exercises "Beer Song", "Proverb", "Difference Of Squares", "Sum Of Multiples", "Grains", "Prime Factors", "Armstrong Numbers" and "Reverse String".

Change contents

  • replacement in rust/proverb/tests/proverb.rs at line 5
    [2.12348][2.12348:12575]()
    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);
    [2.12348]
    [2.12575]
    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);
  • replacement in rust/proverb/tests/proverb.rs at line 18
    [2.12676][2.12676:12962]()
    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);
    [2.12676]
    [2.12962]
    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);
  • replacement in rust/proverb/tests/proverb.rs at line 31
    [2.13005][2.13005:13152]()
    let input = vec!["nail"];
    let expected = String::from("And all for the want of a nail.");
    assert_eq!(build_proverb(&input), expected);
    [2.13005]
    [2.13152]
    let input = vec!["nail"];
    let expected = String::from("And all for the want of a nail.");
    assert_eq!(build_proverb(&input), expected);
  • replacement in rust/proverb/tests/proverb.rs at line 39
    [2.13197][2.13197:13315]()
    let input: Vec<&str> = vec![];
    let expected = String::new();
    assert_eq!(build_proverb(&input), expected);
    [2.13197]
    [2.13315]
    let input: Vec<&str> = vec![];
    let expected = String::new();
    assert_eq!(build_proverb(&input), expected);
  • replacement in rust/proverb/tests/proverb.rs at line 47
    [2.13353][2.13353:13907]()
    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);
    [2.13353]
    [2.13907]
    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);
  • replacement in rust/proverb/tests/proverb.rs at line 66
    [2.13964][2.13964:14312]()
    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);
    [2.13964]
    [2.14312]
    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);
  • replacement in rust/proverb/src/lib.rs at line 2
    [2.14411][2.14411:14485]()
    unimplemented!("build a proverb from this list of items: {:?}", list)
    [2.14411]
    [2.14485]
    match list.first() {
    Some(first) => list
    .windows(2)
    .map(|window| format!("For want of a {} the {} was lost.\n", window[0], window[1]))
    .chain(std::iter::once(format!(
    "And all for the want of a {}.",
    first
    )))
    .collect(),
    _ => String::new(),
    }