Rust 学习笔记:编程练习(一)

Rust 学习笔记:编程练习(一)

  • [Rust 学习笔记:编程练习(一)](#Rust 学习笔记:编程练习(一))
    • [Convert temperatures between Fahrenheit and Celsius](#Convert temperatures between Fahrenheit and Celsius)
    • [Generate the nth Fibonacci number](#Generate the nth Fibonacci number)
    • [Print the lyrics to the Christmas carol "The Twelve Days of Christmas," taking advantage of the repetition in the song](#Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking advantage of the repetition in the song)

Rust 学习笔记:编程练习(一)

Convert temperatures between Fahrenheit and Celsius

华氏度(F)与摄氏度(C)的转换公式为:F = C×1.8 + 32。

经过简单的代数变换可以得到华氏度转摄氏度的公式:C = (F - 32)÷1.8。

rust 复制代码
fn celsius_to_fahrenheit(celsius: f32) -> f32 {
    celsius * 1.8 + 32.0
}

fn fahrenheit_to_celsius(fahrenheit: f32) -> f32 {
    (fahrenheit - 32.0) / 1.8
}

fn main() {
    let celsius: f32 = 0.0;
    println!(
        "{celsius}°C 转换为华氏度是 {}°F",
        celsius_to_fahrenheit(celsius)
    );
    let fahrenheit: f32 = 212.0;
    println!(
        "{fahrenheit}°F 转换为摄氏度是 {}°C",
        fahrenheit_to_celsius(fahrenheit)
    );
}

运行结果:

Generate the nth Fibonacci number

斐波那契数列从第 3 项开始,每一项都等于前两项之和。

具体来说,设F(n)为该数列的第n项(n∈N*),则递推公式为:F(n) = F(n-1) + F(n-2)(n≥3)‌

‌初始值‌:F(0) = 0,F(1) = 1‌

rust 复制代码
fn fibonacci(i: i32) -> i32 {
    if i == 0 {
        return 0;
    }
    if i == 1 {
        return 1;
    }
    fibonacci(i - 1) + fibonacci(i - 2)
}

fn main() {
    for i in 0..=10 {
        println!("F({}) = {}", i, fibonacci(i));
    }
}

运行结果:

Print the lyrics to the Christmas carol "The Twelve Days of Christmas," taking advantage of the repetition in the song

这首歌的歌词,表面上是讲圣诞节的十二天里要做的事,看起来其实就只是一首很可爱的歌而已,好像没有什么太大的含意在里面,其实,不只是这样喔!"圣诞节的十二日"可说是一首寓意极为深远的"启蒙歌曲"。这首圣诞歌最原始是由英国的新教派所写成,由于十六世纪之后的一两百年,英格兰的国会并不承认这个教派,所以他们依法不能传教,或公开从事他们的信仰活动。在当时,身为一位新教徒可是违法的呢!被抓到之后,轻则牢狱之灾,重则被砍头或被吊死都有可能。既然如此,新教派应该如何秘密传教呢?于是,他们便写了这首歌!歌曲乍听之下跟宗教完全不相干,只是在讲十二天里要为圣诞节准备十二种礼物,其实,这十二种礼物暗示着里十二种宗教上的含义。

rust 复制代码
fn main() {
    let times = [
        "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
        "tenth", "eleventh", "twelfth",
    ];
    let items = [
        "a partridge in a pear tree",
        "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",
    ];

    for i in 0..times.len() {
        print!("On the {} day of Christmas, ", times[i]);
        print!("my true love sent to me: ");
        for j in (0..=i).rev() {
            if j == 0 {
                if i != 0 {
                    print!("and ");
                }
                println!("{}. ", items[j]);
            } else {
                print!("{}, ", items[j]);
            }
        }
    }
}

运行结果:

复制代码
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 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 day 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 day 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 day 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. 
相关推荐
LengineerC9 小时前
Rust仿node事件总线的简单实现
设计模式·rust
r***d86510 小时前
Rust宏编程指南
开发语言·后端·rust
星释10 小时前
Rust 练习册 80:Grains与位运算
大数据·算法·rust
wei_shuo12 小时前
zoxide 开源鸿蒙 PC 生态适配实战:Rust 交叉编译与 HNP 打包完整指南
rust·开源鸿蒙·zoxide
大鱼七成饱1 天前
Rust进阶必备:thiserror用法全面解析
rust
Amos_Web1 天前
Rust实战(四):数据持久化、告警配置与Web API —— 构建监控系统的功能闭环
前端·后端·rust
联系QQ:4877392781 天前
Bayes-CNN-LSTM、Bayes-CNN-BiLSTM、Bayes-CNN-GRU、B...
rust
空白诗1 天前
tokei 在鸿蒙PC上的构建与适配
后端·华为·rust·harmonyos
疏狂难除1 天前
尝试rust与python的混合编程(一)
开发语言·后端·python·rust
H***99762 天前
Rust在WebAssembly中的使用
开发语言·rust·wasm