diff options
Diffstat (limited to 'rust/beer-song/src/lib.rs')
| -rw-r--r-- | rust/beer-song/src/lib.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/rust/beer-song/src/lib.rs b/rust/beer-song/src/lib.rs new file mode 100644 index 0000000..990fbbf --- /dev/null +++ b/rust/beer-song/src/lib.rs | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | use itertools::Itertools; | ||
| 2 | |||
| 3 | pub 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 | |||
| 17 | pub 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 | } | ||
