Skip to content

Instantly share code, notes, and snippets.

@lf94
Created October 18, 2017 20:51
Show Gist options
  • Select an option

  • Save lf94/253ec299036fa5009109726dd346563b to your computer and use it in GitHub Desktop.

Select an option

Save lf94/253ec299036fa5009109726dd346563b to your computer and use it in GitHub Desktop.
pub fn verse(n: i32) -> String {
if n > 2 {
format!("{0} bottles of beer on the wall, {0} bottles of beer.\nTake one down and pass it around, {1} bottles of beer on the wall.\n", n, n - 1)
} else if n == 2 {
format!("2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n")
} else if n == 1 {
format!("1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n")
} else {
format!("No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n")
}
}
pub fn sing(start: i32, end: i32) -> String {
let mut song = (end..start+1).rev().map(|i| verse(i)).map(|s| s + "\n").collect::<String>();
if song.ends_with("\n\n") { song.pop(); }
song
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment