Skip to content

Instantly share code, notes, and snippets.

@kronaemmanuel
Last active February 14, 2021 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kronaemmanuel/0ff83e0fe1488700aac41f01a5b49bd2 to your computer and use it in GitHub Desktop.
Save kronaemmanuel/0ff83e0fe1488700aac41f01a5b49bd2 to your computer and use it in GitHub Desktop.
Twelve Days of Christmas Poem on console with Rust
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");
}
}
@wezm
Copy link

wezm commented Mar 19, 2020

You can tidy up the code using a match:

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 ");
            }
            match gift_day {
                1 => println!("a Patridge in a Pear Tree"),
                2 => println!("Two Turtle Doves"),
                3 => println!("Three French Hens"),
                4 => println!("Four Calling Birds"),
                5 => println!("Five Golden Rings"),
                6 => println!("Six Geese a Laying"),
                7 => println!("Seven Swans a Swimming"),
                8 => println!("Eight Maids a Milking"),
                9 => println!("Nine Ladies Dancing"),
                10 => println!("Ten Lords a Leaping"),
                11 => println!("Eleven Pipers Piping"),
                12 => println!("12 Drummers Drumming"),
                _ => {}
            }
        }

        println!("\n");
    }
}

@Curti-s
Copy link

Curti-s commented Feb 14, 2021

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