diff options
| author | Federico I <git@federicoigne.com> | 2020-03-17 01:50:38 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:54:52 +0000 |
| commit | 1dfcb5c8c04f94a02c6527cac848f6411c8af32f (patch) | |
| tree | aeecd5048342725420d34c3e6c2b3331df628dcd /proverb/src/lib.rs | |
| parent | ac194ed95d6b97eef6dd60f8a3524f63fa408553 (diff) | |
| download | exercism-1dfcb5c8c04f94a02c6527cac848f6411c8af32f.tar.gz exercism-1dfcb5c8c04f94a02c6527cac848f6411c8af32f.zip | |
[rust] Proverb
Diffstat (limited to 'proverb/src/lib.rs')
| -rw-r--r-- | proverb/src/lib.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/proverb/src/lib.rs b/proverb/src/lib.rs new file mode 100644 index 0000000..1046553 --- /dev/null +++ b/proverb/src/lib.rs | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | use std::iter; | ||
| 2 | |||
| 3 | pub fn build_proverb(list: &[&str]) -> String { | ||
| 4 | if list.is_empty() { | ||
| 5 | String::new() | ||
| 6 | } else { | ||
| 7 | let reason = |(a, b)| format!("For want of a {} the {} was lost.\n", a, b); | ||
| 8 | let bitter_end = format!("And all for the want of a {}.", list[0]); | ||
| 9 | list.iter() | ||
| 10 | .zip(list.iter().skip(1)) // .window(2) also works here | ||
| 11 | .map(reason) | ||
| 12 | .chain(iter::once(bitter_end)) | ||
| 13 | .collect() | ||
| 14 | } | ||
| 15 | } | ||
