From 739ce5962da60d289594ad1b229158dada2a225e Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Sat, 3 Jul 2021 15:16:51 -0500 Subject: [PATCH] Rewrite to incorporate 2 element window into the slice --- src/lib.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a170b40..0a8a821 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,15 +2,10 @@ pub fn build_proverb(list: &[&str]) -> String { let mut verse = "".to_string(); 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