aboutsummaryrefslogtreecommitdiff
path: root/rust/beer-song/src/lib.rs
blob: 990fbbf0b270a98afaf01f8453409bece6d07bb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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")
}