Skip to content

Instantly share code, notes, and snippets.

@lukad
Created January 2, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukad/9eeccad4d471457bc782fc7d60150b72 to your computer and use it in GitHub Desktop.
Save lukad/9eeccad4d471457bc782fc7d60150b72 to your computer and use it in GitHub Desktop.
pub fn sing(from: i32, to: i32) -> String {
(to..(from+1))
.rev()
.map(|i| verse(i) ).collect::<Vec<_>>()
.join("\n")
}
pub fn verse(n: i32) -> String {
let amount = bottles_of_beer(n);
let action = match n {
0 => "Go to the store and buy some more",
1 => "Take it down and pass it around",
_ => "Take one down and pass it around"
};
let next = match n {
0 => bottles_of_beer(99),
_ => bottles_of_beer(n-1)
}.to_lowercase();
format!("{} on the wall, {}.\n{}, {} on the wall.\n", amount, amount.to_lowercase(), action, next)
}
fn bottles_of_beer(n: i32) -> String {
match n {
0 => "No more bottles of beer".to_owned(),
1 => "1 bottle of beer".to_owned(),
_ => format!("{} bottles of beer", n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment