aboutsummaryrefslogtreecommitdiff
path: root/rust/proverb/src/lib.rs
blob: 10465536cd0fd289e1d86b7c72d152b2434d27a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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()
    }
}