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.