開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Rust 1.36.0 (プログラミング言語)
- プログラミングRust (その他参考書籍)
プログラミング言語Rust 公式ガイド (Steve Klabnik(著)、Carol Nichols(著)、尾崎 亮太(翻訳)、KADOKAWA)の第3章(一般的なプログラミングの概念)の摂氏を華氏に、フィボナッチ数列を生成するプログラムのソースコードのコメント中以外にも日本語を含めて書いてみる。(コメント中以外に日本語文字があるとコンパイルに失敗することがあるとの記述があったから、失敗する場合をちょっとずつ探してみる。)
コード
// 再帰関数
fn fib1(n: i32) -> i32 {
if n <= 1 {
n
} else {
fib1(n - 2) + fib1(n - 1)
}
}
// ループ
fn fib2(n: i32) -> i32 {
if n <= 1 {
n
} else {
let (mut a, mut b): (i32, i32) = (0, 1);
let m = n + 1;
for _ in 2..m {
let t = a;
a = b;
b += t;
}
let c: i32 = b;
c
}
}
fn main() {
for fib in [fib1, fib2].iter() {
for n in 0..11 {
println!("{}", fib(n));
}
}
}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ cargo check Checking sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) warning: unused variable: `i` --> src/main.rs:15:13 | 15 | for i in 2..m { | ^ help: consider prefixing with an underscore: `_i` | = note: #[warn(unused_variables)] on by default warning: function is never used: `fib2` --> src/main.rs:9:1 | 9 | fn fib2(n: i32) -> i32 { | ^^^^^^^^^^^^^^^^^^^^^^ | = note: #[warn(dead_code)] on by default Finished dev [unoptimized + debuginfo] target(s) in 0.14s $ cargo check Checking sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) warning: function is never used: `fib2` --> src/main.rs:9:1 | 9 | fn fib2(n: i32) -> i32 { | ^^^^^^^^^^^^^^^^^^^^^^ | = note: #[warn(dead_code)] on by default Finished dev [unoptimized + debuginfo] target(s) in 0.10s $ cargo check Checking sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) warning: function is never used: `fib2` --> src/main.rs:9:1 | 9 | fn fib2(n: i32) -> i32 { | ^^^^^^^^^^^^^^^^^^^^^^ | = note: #[warn(dead_code)] on by default Finished dev [unoptimized + debuginfo] target(s) in 0.11s $ cargo check Checking sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) error: 2 positional arguments in format string, but there is 1 argument --> src/main.rs:27:23 | 27 | println!("{} {}", fib(n)); | ^^ ^^ error[E0277]: `[fn(i32) -> i32; 2]` is not an iterator --> src/main.rs:25:16 | 25 | for fib in [fib1, fib2] { | ^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it | = help: the trait `std::iter::Iterator` is not implemented for `[fn(i32) -> i32; 2]` = note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]` = note: required by `std::iter::IntoIterator::into_iter` error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0277`. error: Could not compile `sample2`. To learn more, run the command again with --verbose. $ cargo check Checking sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) error[E0277]: `[fn(i32) -> i32; 2]` is not an iterator --> src/main.rs:25:16 | 25 | for fib in [fib1, fib2] { | ^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it | = help: the trait `std::iter::Iterator` is not implemented for `[fn(i32) -> i32; 2]` = note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]` = note: required by `std::iter::IntoIterator::into_iter` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `sample2`. To learn more, run the command again with --verbose. $ cargo check Checking sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) Finished dev [unoptimized + debuginfo] target(s) in 0.11s $ cargo run Compiling sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) Finished dev [unoptimized + debuginfo] target(s) in 0.33s Running `target/debug/sample2` 0 1 1 2 3 5 8 13 21 34 55 0 1 2 1 1 2 3 5 8 13 21 $ cargo run Compiling sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) Finished dev [unoptimized + debuginfo] target(s) in 0.25s Running `target/debug/sample2` 0 1 1 2 3 5 8 13 21 34 55 0 1 1 1 1 2 3 5 8 13 21 $ cargo check Checking sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) Finished dev [unoptimized + debuginfo] target(s) in 0.11s $ cargo run Compiling sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) Finished dev [unoptimized + debuginfo] target(s) in 0.28s Running `target/debug/sample2` 0 1 1 2 3 5 8 13 21 34 55 0 1 1 1 2 3 5 8 13 21 34 $ cargo run Compiling sample2 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample2) Finished dev [unoptimized + debuginfo] target(s) in 0.27s Running `target/debug/sample2` 0 1 1 2 3 5 8 13 21 34 55 0 1 1 2 3 5 8 13 21 34 55 $
0 コメント:
コメントを投稿