back
mail@jonahv.com • rss
christmas carol
background
The final section of the third chapter of the rust book ends with some suggested exercises. The last of which is to write a program to: “Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking advantage of the repetition in the song.”
solution
This was more fun that it looked. Especially trying to identify patterns in the lyrics.
Is this the most optimised version?
Probably not.
fn main() {
let days = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"];
let gifts = [
"Two turtle doves,",
"Three French hens,",
"Four calling birds,",
"Five gold rings,",
"Six geese a-laying,",
"Seven swans a-swimming,",
"Eight maids a-milking,",
"Nine ladies dancing,",
"Ten lords a-leaping,",
"Eleven pipers piping",
"Twelve drummers drumming,"
];
let mut i = 0;
for day in days {
println!("On the {day} of Christmas,");
println!("my true love sent to me");
for j in (0..i).rev() {
let gift = gifts[j];
println!("{gift}");
};
if i > 0 {
println!("And a partridge in a pear tree.")
} else {
println!("A partridge in a pear tree.");
}
println!();
i = i + 1;
}
}
On the first of Christmas,
my true love sent to me
A partridge in a pear tree.
On the second of Christmas,
my true love sent to me
Two turtle doves,
And a partridge in a pear tree.
On the third of Christmas,
my true love sent to me
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the fourth of Christmas,
my true love sent to me
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the fifth of Christmas,
my true love sent to me
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the sixth of Christmas,
my true love sent to me
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the seventh of Christmas,
my true love sent to me
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the eighth of Christmas,
my true love sent to me
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the ninth of Christmas,
my true love sent to me
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the tenth of Christmas,
my true love sent to me
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the eleventh of Christmas,
my true love sent to me
Eleven pipers piping
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
On the twelfth of Christmas,
my true love sent to me
Twelve drummers drumming,
Eleven pipers piping
Ten lords a-leaping,
Nine ladies dancing,
Eight maids a-milking,
Seven swans a-swimming,
Six geese a-laying,
Five gold rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.