Learning Rust — Printing ‘Twelve Days of Christmas’ on console
Tags: Twelve Days Of Christmas, Learning Rust, Console Application, Rust

Photo by Chris Ried on Unsplash

Rust is a rather peculiar language and Twelve Days of Christmas is a rather peculiar poem. It is very simple to learn and involves a lot of repetition which makes it the perfect candidate to be printed out on a console screen via a simple Rust program.

The Rust Programming Language aka The Book is what I’m following to learn Rust these days. Learning from a book like this lets me find a lot more details than I would if I was learning from a youtube tutorial. This problem is listed as an exercise for self at the end of Chapter 3.

Problem: Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking advantage of the repetition in the song.

Here are the lyrics:

On the first day of Christmas my true love sent to me

A partridge in a pear tree On the second day of Christmas my true love sent to me

Two turtle doves

And a partridge in a pear tree On the third day of Christmas my true love sent to me

Three French hens, two turtle doves

And a partridge in a pear tree On the fourth day 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 day 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 day 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 day 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 day 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 day of Christmas my true love sent to me

Nine drummers drumming

On the tenth day of Christmas my true love sent to me

Ten pipers piping Nine drummers drumming, ten pipers piping

Drumming, piping, drumming, piping

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 day of Christmas my true love sent to me

Eleven ladies dancing, ten pipers piping, nine drummers drumming

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 day of Christmas my true love sent to me

Twelve Lords a leaping, eleven ladies dancing, ten pipers piping

Nine, drummers drumming, eight maids a milking

Seven swans a swimming, six geese a laying

And five gold rings, four calling birds, three French hens, two turtle doves

And a partridge in a pear tree, and a partridge in a pear tree

Yeah, just skip over it, here’s how I break down this problem:

for i = 1 to 12

print('On the {i}th day of Christmas my true love sent to me')

for j = i to 1

print({the appropriate gift for the gift day})

Thinking back, I probably should have done this using arrays and stuff so it had better readability but at that time, I was just thinking about how to make it work.

So here’s the final code:

Code for Twelve Days of Christmas Carol with Rust

If this code helped you, be sure to STAR this gist and if you’d like to suggest changes in the code, comment on the gist here.

Best Regards,

Krona Emmanuel 👑