From 02481656966b0a8dfc95cf3c22bcc049660ff7d4 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sat, 26 Dec 2020 17:48:38 +0000 Subject: Move Rust exercises in a subdirectory --- rust/proverb/src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 rust/proverb/src/lib.rs (limited to 'rust/proverb/src') diff --git a/rust/proverb/src/lib.rs b/rust/proverb/src/lib.rs new file mode 100644 index 0000000..1046553 --- /dev/null +++ b/rust/proverb/src/lib.rs @@ -0,0 +1,15 @@ +use std::iter; + +pub fn build_proverb(list: &[&str]) -> String { + if list.is_empty() { + String::new() + } else { + let reason = |(a, b)| format!("For want of a {} the {} was lost.\n", a, b); + let bitter_end = format!("And all for the want of a {}.", list[0]); + list.iter() + .zip(list.iter().skip(1)) // .window(2) also works here + .map(reason) + .chain(iter::once(bitter_end)) + .collect() + } +} -- cgit v1.2.3