Browse Source

Rewrite to incorporate 2 element window into the slice

master
Jim Infield 3 years ago
parent
commit
739ce5962d
  1. 11
      src/lib.rs

11
src/lib.rs

@ -2,15 +2,10 @@ pub fn build_proverb(list: &[&str]) -> String {
let mut verse = "".to_string(); let mut verse = "".to_string();
if !list.is_empty() { if !list.is_empty() {
let nail = list.first().unwrap();
let mut prev = nail;
for i in 1..list.len() {
let next = list.get(i).unwrap();
verse.push_str(&format!("For want of a {} the {} was lost.\n", prev, next));
prev = next;
for item in list.windows(2) {
verse.push_str(&format!("For want of a {} the {} was lost.\n", item[0], item[1]));
} }
verse.push_str(&format!("And all for the want of a {}.", nail));
verse.push_str(&format!("And all for the want of a {}.", list[0]));
} }
verse verse

Loading…
Cancel
Save