diff options
| author | Federico Igne <git@federicoigne.com> | 2020-12-26 17:48:38 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch) | |
| tree | 8e39798fcaf27931d91c2088423fd4e97adcfc2d /rust/proverb/src | |
| parent | 4e2052c4d792540c2f742b2c2a081b11117ed41d (diff) | |
| download | exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip | |
Move Rust exercises in a subdirectory
Diffstat (limited to 'rust/proverb/src')
| -rw-r--r-- | rust/proverb/src/lib.rs | 15 |
1 files changed, 15 insertions, 0 deletions
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 @@ | |||
| 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 | } | ||
