2020-08-16 22:48:06
Rust lang のチュートリアル3章5項の自分の解答
Rust lang のチュートリアル3章5項の自分の解答
以下を実装したプログラムです
- 温度を華氏と摂氏で変換する。
- フィボナッチ数列のn番目を生成する。
- クリスマスキャロルの定番、"The Twelve Days of Christmas"の歌詞を、 曲の反復性を利用して出力する。https://doc.rust-jp.rs/book/second-edition/ch03-05-control-flow.html
以下コードです。 variables というプロジェクトで実装しています。
入力値のオーバーフローは無視しています。例えば、 get_specific_fib(50)
などすると返り値が i32
を超えるのでエラーになります。とくにハンドリングはしていません。
fn main() {
let celsius_to_fahrenheit = celsius_to_fahrenheit(35.5);
println!("celsius_to_fahrenheit: {}", celsius_to_fahrenheit);
let get_specific_fib = get_specific_fib(7);
println!("get_specific_fib: {}", get_specific_fib);
println!("
");
the_twelve_days_of_christmas();
}
fn celsius_to_fahrenheit(celsius: f64) -> f64 {
(celsius * 9.0 / 5.0) + 32.0
}
fn get_specific_fib(index: i32) -> i32 {
if index == 1 {
return 0;
} else if index == 2 {
return 1;
}
let mut first = 0;
let mut second = 1;
let mut cnt = 2;
let mut acc; // acc = 0 にすると何故か warning がでる
loop {
cnt = cnt + 1;
acc = first + second;
if cnt == index {
break;
}
first = second;
second = acc;
}
acc
}
fn the_twelve_days_of_christmas() {
let fix_part = "On the first day of Christmas,
my true love sent to me";
let arr_part = [
"And 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",
];
let song_part = 12;
let mut start = 1;
let mut index;
while start != (song_part-1) {
println!("{}", fix_part);
index = start;
while index >= 1 {
if start == 1 {
println!("A partridge in a pear tree.");
} else {
println!("{}", arr_part[index - 1]);
}
index = index - 1;
}
start = start + 1;
println!("
");
}
}
以下実行結果です
/home/nao000dotcom/variables # cargo run
Compiling variables v0.1.0 (/home/nao000dotcom/variables)
Finished dev [unoptimized + debuginfo] target(s) in 1.21s
Running `target/debug/variables`
celsius_to_fahrenheit: 95.9
get_specific_fib: 8
On the first day of Christmas,
my true love sent to me
A partridge in a pear tree.
On the first day of Christmas,
my true love sent to me
Two turtle doves,
And a partridge in a pear tree.
On the first day of Christmas,
my true love sent to me
Three French hens
Two turtle doves,
And a partridge in a pear tree.
On the first 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 first day of Christmas,
my true love sent to me
Five golden rings
Four calling birds
Three French hens
Two turtle doves,
And a partridge in a pear tree.
On the first day of Christmas,
my true love sent to me
Six geese a-laying
Five golden rings
Four calling birds
Three French hens
Two turtle doves,
And a partridge in a pear tree.
On the first day of Christmas,
my true love sent to me
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.
On the first day of Christmas,
my true love sent to me
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.
On the first 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 golden rings
Four calling birds
Three French hens
Two turtle doves,
And a partridge in a pear tree.
On the first 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 golden rings
Four calling birds
Three French hens
Two turtle doves,
And a partridge in a pear tree.
/home/nao000dotcom/variables #