システムプログラミングのための言語である Rust の解説書。
この本の題名は「実践 Rust プログラミング入門」である。本文を読むと、 このうちの「実践」および「プログラミング」やに重点が置かれていて、 「入門」の比重は軽い。パート 1 が入門篇で、ここで Rust の文法が駆け足で説明されている。
p.146 以降で解説されている逆ポーランド記法の数式を計算するアプリケーションを作ってみた。 p.148 で紹介されているサードパーティー製クレート clap は、本書では 3.00-beta.1 が使われているが、 私が 2023/05/24 に clap を導入してみたら最新版は 4.3.0 だった。
ということは、clap の Builder パターンで記述された pp.148-149 のプログラムは、そのままでは動かないだろう。どんなエラーが出るだろうか。
> cargo run (中略) error[E0432]: unresolved import `clap::App` --> src\main.rs:11:12 | 11 | use clap::{App, Arg}; | ^^^ no `App` in the root error[E0599]: no function or associated item named `with_name` found for struct `Arg` in the current scope --> src\main.rs:19:18 | 19 | Arg::with_name("formula_file") | ^^^^^^^^^ function or associated item not found in `Arg` error[E0599]: no function or associated item named `with_name` found for struct `Arg` in the current scope --> src\main.rs:26:18 | 26 | Arg::with_name("verbose") | ^^^^^^^^^ function or associated item not found in `Arg` Some errors have detailed explanations: E0432, E0599. For more information about an error, try `rustc --explain E0432`. (後略)
ではこうしよう。本書のサンプルプログラムは下記にあるので、それをもってきた。
https://github.com/forcia/rustbook/
ここの ch04/4-1/samplecli-builder/src/main.rs に差し替えた。
こんどはどうか。
> cargo run (中略) error[E0432]: unresolved import `clap::App` --> src\main.rs:11:17 | 11 | use clap::{arg, App}; | ^^^ no `App` in the root For more information about this error, try `rustc --explain E0432`.
https://docs.rs/clap/latest/clap/_tutorial/index.html
を見ると、use clap::{arg, App}; ではなく、use clap::{arg, Command}; とするようだ。だから、インスタンスを作るのも、App::new ではなく、Command::new を使うようだ。
こんどはどうか。
> cargo run (中略) error[E0599]: no method named `value_of` found for struct `ArgMatches` in the current scope --> src\main.rs:22:19 | 22 | match matches.value_of("FILE") { | ^^^^^^^^ method not found in `ArgMatches` error[E0599]: no method named `is_present` found for struct `ArgMatches` in the current scope --> src\main.rs:26:27 | 26 | let verbose = matches.is_present("verbose"); | ^^^^^^^^^^ help: there is a method with a similar name: `args_present` (後略)
ここまで来てあきらめた。Builder パターンで記述する方法とは別に、本書では derive マクロを使った記述方法も解説されている。
それに、下記の clap のチュートリアルを見ても、
https://docs.rs/clap/latest/clap/_faq/index.html#when-should-i-use-the-builder-vs-derive-apis
When should I use the builder vs derive APIs? という問に対して、
Our default answer is to use the Derive API: という答がある。derive マクロを使う方法がよさそうだ。
そこで、ここの ch04/4-1/samplecli-derivemacro/src/main.rsに差し替えた。
> cargo run (中略) error: cannot find derive macro `Parser` in this scope --> src\main.rs:13:10 | 13 | #[derive(Parser, Debug)] | ^^^^^^ | note: `Parser` is imported here, but it is only a trait, without a derive macro (後略)
> cargo add clap
としたのがよくなかったようだ。
https://docs.rs/clap/latest/clap/index.html#example
によれば、
> cargo add clap --features derive
とすべきだった。今からでも遅くないだろう。上記のように --features derive を指定した。こんどはどうか。
エラーが出なかった。万歳。clap に関しては以下も本書にある通りの結果が得られた。 しかし、肝心のアプリに関しては時間切れとなった。
書名 | 実践 Rust プログラミング入門 |
著者 | 初田直也、山口聖弘、吉川哲史 、豊田優貴、松本健太郎、原将己 、中村謙弘 |
発行日 | 2020 年 9 月 1 日 第 1 版第 1 刷 |
発行元 | 秀和システム |
定価 | 3600円(税別) |
サイズ | |
ISBN | 978-4-7980-6170-2 |
備考 | 越谷市立図書館で借りて読む |
NDC | 007.6 |
まりんきょ学問所 > コンピュータの部屋 > コンピュータの本 > Rust > 初田直也、山口聖弘、吉川哲史、豊田優貴、松本健太郎、原将己、中村謙弘 : 実践 Rust プログラミング入門