aboutsummaryrefslogtreecommitdiff
path: root/beer-song/src/lib.rs
diff options
context:
space:
mode:
authorFederico I <git@federicoigne.com>2020-03-17 00:53:30 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:54:45 +0000
commitac194ed95d6b97eef6dd60f8a3524f63fa408553 (patch)
treea7e5ae82dc02475ff711cd5e39ae1e4817276624 /beer-song/src/lib.rs
parenta4b6e4b2e898b7efa0b44b490bd731557b0cc41b (diff)
downloadexercism-ac194ed95d6b97eef6dd60f8a3524f63fa408553.tar.gz
exercism-ac194ed95d6b97eef6dd60f8a3524f63fa408553.zip
[rust] Beer Song
Diffstat (limited to 'beer-song/src/lib.rs')
-rw-r--r--beer-song/src/lib.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/beer-song/src/lib.rs b/beer-song/src/lib.rs
new file mode 100644
index 0000000..990fbbf
--- /dev/null
+++ b/beer-song/src/lib.rs
@@ -0,0 +1,20 @@
1use itertools::Itertools;
2
3pub fn verse(n: u32) -> String {
4 match n {
5 0 => format!( "No more bottles of beer on the wall, no more bottles of beer.\n\
6 Go to the store and buy some more, 99 bottles of beer on the wall.\n"),
7 1 => format!( "1 bottle of beer on the wall, 1 bottle of beer.\n\
8 Take it down and pass it around, no more bottles of beer on the wall.\n"),
9 2 => format!( "2 bottles of beer on the wall, 2 bottles of beer.\n\
10 Take one down and pass it around, 1 bottle of beer on the wall.\n"),
11 _ => format!( "{} bottles of beer on the wall, {} bottles of beer.\n\
12 Take one down and pass it around, {} bottles of beer on the wall.\n",
13 n, n, n - 1)
14 }
15}
16
17pub fn sing(start: u32, end: u32) -> String {
18 // Note: call to `join` can be substituted with `.intersperse(String::from("\n")).collect()`
19 (end..=start).rev().map(|x| verse(x)).join("\n")
20}