Add twelve days of christmas example
This commit is contained in:
parent
ef64ba88ab
commit
ad04552e39
7
twelve_days_of_christmas/Cargo.toml
Normal file
7
twelve_days_of_christmas/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "twelve_days_of_christmas"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
58
twelve_days_of_christmas/src/main.rs
Normal file
58
twelve_days_of_christmas/src/main.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
const ORDINALS: [&str; 12] = [
|
||||||
|
"first",
|
||||||
|
"second",
|
||||||
|
"third",
|
||||||
|
"fourth",
|
||||||
|
"fifth",
|
||||||
|
"sixth",
|
||||||
|
"seventh",
|
||||||
|
"eighth",
|
||||||
|
"ninth",
|
||||||
|
"tenth",
|
||||||
|
"eleventh",
|
||||||
|
"twelfth",
|
||||||
|
];
|
||||||
|
|
||||||
|
const GIFTS: [&str; 12] = [
|
||||||
|
"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 golden rings",
|
||||||
|
"Four calling birds",
|
||||||
|
"Three french hens",
|
||||||
|
"Two turtle doves",
|
||||||
|
"and a partridge in a pear tree.",
|
||||||
|
];
|
||||||
|
|
||||||
|
fn format_verse(day: &str, gifts: &str) -> String {
|
||||||
|
format!("On the {} day of Christmas, my true love gave to me: \n{}\n", day, gifts)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
for n in 0..12 {
|
||||||
|
let verse: String = match n {
|
||||||
|
0 => format_verse("first", "A partridge in a pear tree."),
|
||||||
|
_ => {
|
||||||
|
// The highest index is 11, and we want
|
||||||
|
// the highest index to start, so we can
|
||||||
|
// easily have the cumulative list each verse.
|
||||||
|
let gift_index = 11 - n;
|
||||||
|
|
||||||
|
// Get the slice of the array of gifts, from
|
||||||
|
// the index to the end of the array
|
||||||
|
let gifts = &GIFTS[gift_index..]
|
||||||
|
.to_vec() // So we have the join method that returns a string
|
||||||
|
.join(",\n");
|
||||||
|
|
||||||
|
format_verse(ORDINALS[n], gifts)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// For some reason, the println! macro only takes string literals
|
||||||
|
println!("{}", verse);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user