Created
January 2, 2018 15:33
-
-
Save lukad/9eeccad4d471457bc782fc7d60150b72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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