Created
July 7, 2017 22:12
-
-
Save ledoyen/a1d31601cffddcca389f8567ee23c786 to your computer and use it in GitHub Desktop.
99 Bottles of Beer in Rust
This file contains 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
fn main() { | |
bottlesong_rec(99); | |
bottlesong_loop(99); | |
} | |
fn bottlesong_rec(bottle_number: i32) { | |
if bottle_number > 0 { | |
println!("{0} bottle{1} of beer on the wall, {0} bottle{1} of beer.", bottle_number, compute_plural(bottle_number)); | |
if bottle_number == 1 { | |
println!("Take one down and pass it around, no more bottles of beer on the wall.\n\n\ | |
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."); | |
} else { | |
println!("Take one down and pass it around, {} bottle{} of beer on the wall.\n", bottle_number - 1, compute_plural(bottle_number-1)); | |
bottlesong_rec(bottle_number - 1); | |
} | |
} | |
} | |
fn bottlesong_loop(start_bottle_number: i32) { | |
for bottle_number in start_bottle_number..1 { | |
if bottle_number == 1 { | |
println!("Take one down and pass it around, no more bottles of beer on the wall.\n\n\ | |
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."); | |
} else { | |
println!("Take one down and pass it around, {} bottle{} of beer on the wall.\n", bottle_number - 1, compute_plural(bottle_number-1)); | |
} | |
} | |
} | |
fn compute_plural(count: i32) -> String {(if count == 1 {""} else {"s"}).to_string()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment