Last active
February 14, 2021 14:46
-
-
Save kronaemmanuel/0ff83e0fe1488700aac41f01a5b49bd2 to your computer and use it in GitHub Desktop.
Twelve Days of Christmas Poem on console with 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() { | |
twelve_days_of_xmas(); | |
} | |
fn twelve_days_of_xmas(){ | |
let days = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"]; | |
for (day_num, day) in days.iter().enumerate() { | |
println!("For the {} day of Christmas my true love sent to me", day); | |
for gift_day in (1..(day_num + 1)).rev(){ | |
if gift_day == 1 && day_num != 1 { | |
println!("and "); | |
} | |
if gift_day == 1 { | |
println!("a Patridge in a Pear Tree"); | |
} else if gift_day == 2 { | |
println!("Two Turtle Doves"); | |
} else if gift_day == 3 { | |
println!("Three French Hens"); | |
} else if gift_day == 4 { | |
println!("Four Calling Birds"); | |
} else if gift_day == 5 { | |
println!("Five Golden Rings"); | |
} else if gift_day == 6 { | |
println!("Six Geese a Laying"); | |
} else if gift_day == 7 { | |
println!("Seven Swans a Swimming"); | |
} else if gift_day == 8 { | |
println!("Eight Maids a Milking"); | |
} else if gift_day == 9 { | |
println!("Nine Ladies Dancing"); | |
} else if gift_day == 10 { | |
println!("Ten Lords a Leaping"); | |
} else if gift_day == 11 { | |
println!("Eleven Pipers Piping"); | |
} else if gift_day == 12 { | |
println!("12 Drummers Drumming"); | |
} | |
} | |
println!("\n"); | |
} | |
} |
Thanks @kronaemmanuel, you really helped me out on this one.
However, the output of the first day seems to be wrong.
A quick fix would be to add another element in the days
array, so this would be like:
let days = [ "-", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can tidy up the code using a
match
: