Exercise Chapter 3 in The Book of Rust

Exercise 1: Convert temperatures between Fahrenheit and Celsius.


fn f_to_c(degree: i32) -> i32{
    ((degree as f64 - 32.) * 5. / 9.).round() as i32
}
fn c_to_f(degree: i32) -> i32{
    (degree as f64 / 5. * 9. + 32.).round() as i32
}

Exercise 2: Generate the nth Fibonacci number.

fn fibonacci(n: u32) -> u32{
    if n == 0 { return 0 };
    if n == 1 { return 1 };
    fibonacci(n-2) + fibonacci(n-1)
}

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

fn the_12_of_days_of_xmas(){
    let days = [
        "first", "second", "third", "fourth",
        "fifth", "sixth", "seventh", "eighth",
        "ninth", "tenth", "eleventh", "twelfth"];
    let presents = [
        "a partridge in a pear tree", "two turtle doves","three French hens",
        "four calling birds","five golden 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"];

    for index in 0..12 {
        println!("{}.",index+1);
        println!("On the {} day of Christmas my true love sent to me", days[index]);
        for j in 0..index+1 {
            if j == index{
                println!("{}.",presents[index-j])
            }else{
                println!("{},",presents[index-j])
            }
        }
    }
}