From ac194ed95d6b97eef6dd60f8a3524f63fa408553 Mon Sep 17 00:00:00 2001 From: Federico I Date: Tue, 17 Mar 2020 00:53:30 +0000 Subject: [rust] Beer Song --- beer-song/src/lib.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 beer-song/src/lib.rs (limited to 'beer-song/src/lib.rs') 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 @@ +use itertools::Itertools; + +pub fn verse(n: u32) -> String { + match n { + 0 => format!( "No more bottles of beer on the wall, no more bottles of beer.\n\ + Go to the store and buy some more, 99 bottles of beer on the wall.\n"), + 1 => format!( "1 bottle of beer on the wall, 1 bottle of beer.\n\ + Take it down and pass it around, no more bottles of beer on the wall.\n"), + 2 => format!( "2 bottles of beer on the wall, 2 bottles of beer.\n\ + Take one down and pass it around, 1 bottle of beer on the wall.\n"), + _ => format!( "{} bottles of beer on the wall, {} bottles of beer.\n\ + Take one down and pass it around, {} bottles of beer on the wall.\n", + n, n, n - 1) + } +} + +pub fn sing(start: u32, end: u32) -> String { + // Note: call to `join` can be substituted with `.intersperse(String::from("\n")).collect()` + (end..=start).rev().map(|x| verse(x)).join("\n") +} -- cgit v1.2.3