Rust 1.41 Toolset の使用
Rust 1.41 Toolset のインストールおよび使用
概要
第1章 Rust
1.1. Rust ツールセットについて
Rust ツールセットは、Red Hat Enterprise Linux プラットフォームで開発者向けの Red Hat 製品です。これは、Rust プログラミング言語コンパイラー rustc、Cargo ビルドツールおよび依存マネージャー、rustfmt ツール、必須のライブラリーを提供します。cargo-vendor
パッケージは cargo
コマンドに組み込まれていますが、その使用方法は変わりません。
Rust ツールセットは、Red Hat Enterprise Linux 7 の一部として配布されており、Red Hat Enterprise Linux 8 でモジュールとして利用できます。
以下のコンポーネントは、Rust ツールセットの一部として利用できます。
表1.1 Rust コンポーネント
名前 | バージョン | 説明 |
---|---|---|
| 1.41 | LLVM 用の Rust コンパイラーフロントエンド。 |
| 1.41 | Rust のビルドシステムおよび依存関係マネージャー。 |
| 1.41 | Rust コードの自動フォーマットを行うためのツール。 |
1.2. 互換性
Rust ツールセットは、Red Hat Enterprise Linux 7 および Red Hat Enterprise Linux 8 では、以下のアーキテクチャーで利用できます。
- 64 ビット Intel および AMD アーキテクチャー
- 64 ビット ARM アーキテクチャー
- IBM Power Systems アーキテクチャー
- IBM Power Systems アーキテクチャーのリトルエンディアンバリアント
- IBM Z Systems アーキテクチャー
1.3. Red Hat Enterprise Linux 7 で Rust ツールセットへのアクセス
本章では、Red Hat Enterprise Linux 7 システムに Rust ツールセットをインストールする前に実行する手順を説明します。以下の手順をすべて実行して、Red Hat Developer Toolset レポジトリーにアクセスできるサブスクリプションをアタッチし、Red Hat Developer Tools および Red Hat Software Collections レポジトリーを有効化します。
前提条件
wget
がシステムにインストールされていることを確認します。このツールは、デフォルトの Red Hat Enterprise Linux リポジトリーから利用できます。これをインストールするには、root で以下のコマンドを入力します。# yum install wget
手順
サーバーから最新のサブスクリプションデータを取得します。
# subscription-manager refresh
以下のコマンドを使用して、システムを登録します。
# subscription-manager register
また、Red Hat Subscription Management ドキュメントの Registering and Unregistering a System の適切な手順に従って、システムを登録することもできます。
システムで利用可能なすべてのサブスクリプションの一覧を表示し、サブスクリプションのプール ID を特定します。
# subscription-manager list --available
このコマンドは、サブスクリプション名、一意の ID、有効期限などの関連情報を表示します。プール ID は、
Pool ID
で始まる行に一覧表示されます。Red Hat Developer Tools
リポジトリーへのアクセスを提供するサブスクリプションを割り当てます。直前の手順で特定したプール ID を使用します。# subscription-manager attach --pool=<appropriate pool ID from the subscription>
システムに割り当てられているサブスクリプションの一覧を確認します。
# sudo subscription-manager list --consumed
rhel-7-variant-devtools-rpms
レポジトリーを有効化します。# subscription-manager repos --enable rhel-7-variant-devtools-rpms
variant
を、Red Hat Enterprise Linux システムのバリアント (server
またはworkstation
) に置き換えます。Red Hat Enterprise Linux Server を使用して、最も幅広い開発ツールにアクセスすることを検討してください。
rhel-variant-rhscl-7-rpms
レポジトリーを有効化します。# subscription-manager repos --enable rhel-variant-rhscl-7-rpms
variant
を、Red Hat Enterprise Linux システムのバリアント (server
またはworkstation
) に置き換えます。Red Hat Developer Tools GPG キーをシステムに追加します。
# cd /etc/pki/rpm-gpg # wget -O RPM-GPG-KEY-redhat-devel https://www.redhat.com/security/data/a5787476.txt # rpm --import RPM-GPG-KEY-redhat-devel
サブスクリプションがシステムに割り当てられ、リポジトリーが有効になったら、「Rust ツールセットのインストール」 の説明に従って Rust ツールセットをインストールします。
関連情報
- Red Hat Subscription Management を使用してシステムを登録し、サブスクリプションに関連付ける方法は、Red Hat Subscription Management のガイドを参照してください。
1.4. Rust ツールセットのインストール
Red Hat Enterprise Linux システムに Rust ツールセットをインストールするには、以下の手順を実行します。
Rust ツールセットを Red Hat Enterprise Linux 7 システムにインストールするには、Red Hat Developer Tool コンテンツセットへのアクセスを提供する有効なサブスクリプションが必要です。Red Hat Enterprise Linux 7 システムを適切なサブスクリプションに関連付け、Rust ツールセットにアクセスする方法は、「Red Hat Enterprise Linux 7 で Rust ツールセットへのアクセス」 を参照してください。
Rust ツールセットをインストールする前に、利用可能なすべての Red Hat Enterprise Linux 更新をインストールします。
オペレーティングシステムの Rust ツールセットに含まれるすべてのコンポーネントをインストールします。
Red Hat Enterprise Linux 7 で、
rust-toolset-1.41
パッケージをインストールします。# yum install rust-toolset-1.41
Red Hat Enterprise Linux 8 に、
rust-toolset
モジュールをインストールします。# yum module install rust-toolset
これにより、すべての開発およびデバッグツール、および依存するパッケージがシステムにインストールされます。特に、Rust ツールセットには、LLVM Toolset の依存関係があります。
インストール可能なドキュメント
HTML 形式で、The Rust Programming Language および API ドキュメントをインストールします。
Red Hat Enterprise Linux 7 で、
rust-toolset-1.41-rust-doc
パッケージをインストールします。# yum install rust-toolset-1.41-rust-doc
本書は
/opt/rh/rust-toolset-1.41/root/usr/share/doc/rust/html/index.html
で利用できます。すべてのクレデンシャルの API ドキュメントは、
/opt/rh/rust-toolset-1.41/root/usr/share/doc/rust/html/std/index.html
の HTML 形式で利用できます。Red Hat Enterprise Linux 8 で、
rust-doc
パッケージをインストールします。# yum install rust-doc
本書は
/usr/share/doc/rust/html/index.html
で利用できます。すべてのクレデンシャルの API ドキュメントは、
/usr/share/doc/rust/html/std/index.html
の HTML 形式で入手できます。
オンラインドキュメント
- Rust ドキュメント: Rust プロジェクトが提供するオンラインドキュメント
第2章 Cargo
Cargo は、Rust プログラミング言語を使用して開発するためのツールです。Cargo は以下のようになります。
Rust コンパイラー rustc のビルドツールおよびフロントエンド。
注記rustc よりも Cargo の使用を検討してください。
パッケージおよび依存関係マネージャー。
Cargo により、Rust プロジェクトは特定のバージョン要件で依存関係を宣言できます。Cargo は完全な依存関係グラフを解決し、必要に応じてパッケージをダウンロードし、プロジェクト全体をビルドおよびテストします。
Rust ツールセットには、Cargo 1.41 が同梱されています。
2.1. 新しい Rust プロジェクトの作成
コマンドラインで Rust プログラムを作成するには、以下のように cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo new --bin project_name'
Red Hat Enterprise Linux 8 の場合:
$ cargo new --bin project_name
Cargo パッケージ内のディレクトリー構造とファイル配置を定義する特定の規則があります。cargo new
コマンドを実行すると、マニフェストとプロジェクトファイル両方のパッケージディレクトリー構造とテンプレートが生成されます。デフォルトでは、パッケージの root ディレクトリーで新しい Git リポジトリーも初期化します。
バイナリープログラムの場合は、Cargo.toml
という名前のテキストファイルを含むディレクトリー project_name
と、main.rs
という名前のテキストファイルを含むサブディレクトリー src
を作成します。
プロジェクトを設定し、依存関係を追加するには、マニフェストファイル Cargo.toml
を編集します。このファイルには、プロジェクトのコンパイル時に必要なすべてのメタデータが含まれます。「Rust プロジェクト依存関係の設定」 を参照してください。
プロジェクトコードを編集するには、必要に応じて、メインの実行可能ファイル main.rs
を編集し、src
サブディレクトリーに新しいソースファイルを追加します。
上記のコマンドでは、オプション --bin
を使用してバイナリープログラムを作成しました。プログラムの代わりに Cargo パッケージのライブラリーを作成するには、コマンドラインで cargo
ツールを実行して、以下のように --lib
オプションを渡します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo new --lib project_name'
Red Hat Enterprise Linux 8 の場合:
$ cargo new --lib project_name
前の例のように、src
サブディレクトリーにある lib.rs
という名前のライブラリーを含むルートディレクトリーを含む root ディレクトリー project_name
でパッケージディレクトリー構造が作成されます。
例2.1 Cargo を使用した Rust プロジェクトの作成
helloworld
という名前の新しい Rust プロジェクトを作成します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo new --bin helloworld' Created binary (application)
helloworld
projectRed Hat Enterprise Linux 8 の場合:
$ cargo new --bin helloworld Created binary (application)
helloworld
project
結果を確認します。
$ cd helloworld $ tree . ├── Cargo.toml └── src └── main.rs 1 directory, 2 files $ cat src/main.rs fn main() { println!("Hello, world!"); }
ディレクトリー helloworld
がプロジェクト用に作成されます。これは、プロジェクトメタデータを追跡するためのファイル Cargo.toml
、メインのソースコードファイル main.rs
が含まれるサブディレクトリー src
とともに作成されます。
ソースコードファイル main.rs
は Cargo によって、サンプル hello world プログラムに初期化されています。
tree ツールは、デフォルトの Red Hat Enterprise Linux リポジトリーから利用できます。インストールするには、以下を行います。
# yum install tree
2.2. Rust プロジェクトのビルド
コマンドラインで Rust プロジェクトをビルドするには、プロジェクトディレクトリーに移動し、以下のように cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo build'
Red Hat Enterprise Linux 8 の場合:
$ cargo build
これにより、cargo build
を実行すると、プロジェクトのすべての依存関係が解決され、不足している依存関係がダウンロードされ、rustc コンパイラーを使用してプロジェクトをコンパイルします。
デフォルトでは、プロジェクトはデバッグモードでビルドおよびコンパイルされます。このモードは、ソースコードを迅速にコンパイルする必要があり、コードの最適化が必要ない場合に、通常の開発およびデバッグに使用されます。
リリースモードでプロジェクトをビルドするには、以下のように --release
オプションを指定して cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo build --release'
Red Hat Enterprise Linux 8 の場合:
$ cargo build --release
ソースコードの最適化により、このモードでコンパイル時間が長くなります。その結果、コンパイルされたバイナリーはより速く実行されます。このモードを使用して、リリースと実稼働環境に適した最適化されたアーティファクトを生成します。
例2.2 Cargo を使用した Rust プロジェクトのビルド
この例では、例2.1「Cargo を使用した Rust プロジェクトの作成」 に従って Rust プロジェクト helloworld
が正常に作成されていることを前提とします。
helloworld
ディレクトリーに移動し、プロジェクトをビルドします。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo build' Compiling helloworld v0.1.0 (file:///home/vslavik/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.51 secs
Red Hat Enterprise Linux 8 の場合:
$ cargo build Compiling helloworld v0.1.0 (file:///home/vslavik/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.51 secs
結果を確認します。
$ tree . ├── Cargo.lock ├── Cargo.toml ├── src │ └── main.rs └── target └── debug ├── build ├── deps │ └── helloworld-b7c6fab39c2d17a7 ├── examples ├── helloworld ├── helloworld.d ├── incremental └── native 8 directories, 6 files
ディレクトリー target
から開始して、サブディレクトリー構造が作成されました。このプロジェクトはデバッグモードでビルドされているため、実際のビルド出力は追加のサブディレクトリー debug
に含まれます。実際の実行ファイルは target/debug/helloworld
です。
tree ツールは、デフォルトの Red Hat Enterprise Linux リポジトリーから利用できます。インストールするには、以下を行います。
# yum install tree
2.3. Rust プロジェクトをコンパイルすることの確認
cargo が管理する Rust プログラムを構築するには、コマンドラインでプロジェクトディレクトリーに移動し、以下のように cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo check'
Red Hat Enterprise Linux 8 の場合:
$ cargo check
実行可能ファイルを構築する必要がない場合に、cargo build
コマンドの代わりに cargo check
コマンドを使用して Rust プログラムの有効性を確認することを検討してください。cargo check
コマンドは、実行可能なコードを生成しないため、cargo build
コマンドを使用した完全なプロジェクトビルドよりも高速です。
デフォルトでは、プロジェクトはデバッグモードでチェックされます。リリースモードでプロジェクトを確認するには、以下のように --release
オプションを指定して cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo check --release'
Red Hat Enterprise Linux 8 の場合:
$ cargo check --release
例2.3 Rust プロジェクトを Cargo でコンパイルすることの確認
この例では、例2.2「Cargo を使用した Rust プロジェクトのビルド」 に従って Rust プロジェクトの helloworld
が正常にビルドされていることを前提とします。
helloworld
ディレクトリーに移動し、プロジェクトを確認します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo check' Compiling helloworld v0.1.0 (file:///home/vslavik/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.5 secs
Red Hat Enterprise Linux 8 の場合:
$ cargo check Compiling helloworld v0.1.0 (file:///home/vslavik/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.5 secs
プロジェクトをチェックし、cargo build
コマンドの出力と似た出力が表示されます。ただし、実行ファイルは生成されません。これを確認するには、現在の時間を実行ファイルのタイムスタンプと比較します。
$date
Fri Oct 13 08:53:21 CEST 2017 $ls -l target/debug/helloworld
-rwxrwxr-x. 2 vslavik vslavik 252624 Oct 13 08:48 target/debug/helloworld
2.4. Rust プログラムの実行
コマンドラインで Cargo によってプロジェクトとして管理される Rust プログラムを実行するには、以下のようにプロジェクトディレクトリーに移動し、cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo run'
Red Hat Enterprise Linux 8 の場合:
$ cargo run
プログラムがビルドされていない場合、Cargo はプログラムを実行する前にビルドを実行します。
開発中は、Cargo を使用して Rust プログラムを実行することを検討してください。これは、ビルドモードとは独立して、出力パスを正しく解決します。
デフォルトでは、プロジェクトはデバッグモードでビルドされます。リリースモードでプロジェクトをビルドするには、以下のように --release
オプションを指定して cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo run --release'
Red Hat Enterprise Linux 8 の場合:
$ cargo run --release
例2.4 Cargo を使用した Rust プログラムの実行
この例では、例2.2「Cargo を使用した Rust プロジェクトのビルド」 に従って Rust プロジェクトの helloworld
が正常にビルドされていることを前提とします。
helloworld
ディレクトリーに移動し、プロジェクトを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo run' Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs Running
target/debug/helloworld
Hello, world!Red Hat Enterprise Linux 8 の場合:
$ cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs Running
target/debug/helloworld
Hello, world!
Cargo はまずプロジェクトを再構築し、作成された実行ファイルを実行します。
この例では、最後のビルド以降にソースコードに変更がありませんでした。そのため、Cargo は実行可能ファイルを再構築する必要はなく、現時点では受け入れるだけでした。
2.5. Rust プロジェクトのテスト
コマンドラインで Cargo プロジェクトでテストを実行するには、プロジェクトディレクトリーに移動し、以下のように cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo test'
Red Hat Enterprise Linux 8 の場合:
$ cargo test
デフォルトでは、プロジェクトはデバッグモードでテストされています。リリースモードでプロジェクトをテストするには、以下のように --release
オプションを指定して cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo test --release'
Red Hat Enterprise Linux 8 の場合:
$ cargo test --release
例2.5 Cargo での Rust プロジェクトのテスト
この例では、例2.2「Cargo を使用した Rust プロジェクトのビルド」 に従って Rust プロジェクトの helloworld
が正常にビルドされていることを前提とします。
helloworld
ディレクトリーに移動し、以下のソースコードが含まれるように src/main.rs
ファイルを編集します。
fn main() { println!("Hello, world!"); } #[test] fn my_test() { assert_eq!(21+21, 42); }
テスト対象の関数 my_test
が追加されました。
ファイルを保存し、テストを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo test' Compiling helloworld v0.1.0 (file:///home/vslavik/Documentation/rusttest/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.26 secs Running target/debug/deps/helloworld-9dd6b83647b49aec running 1 test test my_test ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
Red Hat Enterprise Linux 8 の場合:
$ cargo test Compiling helloworld v0.1.0 (file:///home/vslavik/Documentation/rusttest/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.26 secs Running target/debug/deps/helloworld-9dd6b83647b49aec running 1 test test my_test ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
Cargo はまずプロジェクトを再構築し、次にプロジェクトにあるテストを実行します。helloworld
プロジェクトはテスト my_test
を正常に渡しました。
2.6. Rust プロジェクト依存関係の設定
Cargo プロジェクトの依存関係を指定するには、プロジェクトディレクトリーの Cargo.toml
ファイルを編集します。このセクション [dependencies]
には、プロジェクトの依存関係の一覧が含まれます。各依存関係は、次の形式で新しい行に一覧表示されます。
crate_name = version
Rust コードパッケージは crates と呼ばれます。
例2.6 プロジェクトへの依存関係の追加および cargo で構築
この例では、例2.2「Cargo を使用した Rust プロジェクトのビルド」 に従って Rust プロジェクトの helloworld
が正常にビルドされていることを前提とします。
helloworld
ディレクトリーに移動し、以下のソースコードが含まれるように src/main.rs
ファイルを編集します。
extern crate time; fn main() { println!("Hello, world!"); println!("Time is: {}", time::now().rfc822()); }
このコードには、外部のクレート time
が必要になりました。以下のコードが含まれるように Cargo.toml
ファイルを編集して、この依存関係をプロジェクト設定に追加します。
[package] name = "helloworld" version = "0.1.0" authors = ["Your Name <yourname@example.com>"] [dependencies] time = "0.1"
cargo run
コマンドを実行してプロジェクトをビルドし、作成された実行ファイルを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo run' Updating registry `https://github.com/rust-lang/crates.io-index` Downloading time v0.1.38 Downloading libc v0.2.32 Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs Running `target/debug/helloworld` Hello, world! Time is: Fri, 13 Oct 2017 11:08:57
Red Hat Enterprise Linux 8 の場合:
$ cargo run Updating registry `https://github.com/rust-lang/crates.io-index` Downloading time v0.1.38 Downloading libc v0.2.32 Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs Running `target/debug/helloworld` Hello, world! Time is: Fri, 13 Oct 2017 11:08:57
Cargo は time
crate とその依存関係 (crate libc
) をダウンロードし、ローカルに保管し、依存関係を含むすべてのプロジェクトのソースコードをビルドし、最終的に生成される実行ファイルを実行します。
関連情報
- 依存関係の指定: 公式の Cargo ドキュメント。
2.7. Rust プロジェクトのドキュメントの構築
rustdoc で rustdoc
コマンドを使用することを検討してください。このコマンド cargo doc
は、rustdoc ユーティリティーを使用します。
cargo doc
は、パブリック関数、変数、メンバーに対してのみ、ドキュメントコメントを抽出します。
Rust コードには、ドキュメントへの抽出用のコメントを含めることができます。コメントは Markdown 言語に対応しています。
Cargo ツールを使用してプロジェクトのドキュメンテーションを構築するには、プロジェクトディレクトリーに移動し、cargo
ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'cargo doc --no-deps'
Red Hat Enterprise Linux 8 の場合:
$ cargo doc --no-deps
これにより、プロジェクトのソースコードにある特別なコメントから保存したドキュメントが抽出され、HTML 形式でドキュメントが書き込まれます。
-
サードパーティーライブラリーなど、生成されたドキュメントの依存関係を含める
--no-deps
オプションを省略します。 -
ブラウザーで生成されたドキュメントを開く
--open
オプションを追加します。
例2.7 Cargo を使用した Rust プロジェクトのドキュメントの構築
この例では、例2.6「プロジェクトへの依存関係の追加および cargo で構築」 に従って依存関係で Rust プロジェクト helloworld
を正常にビルドしていることを前提としています。
helloworld
ディレクトリーに移動し、以下のソースコードが含まれるように src/main.rs
ファイルを編集します。
//! This is a hello-world program. extern crate time; /// Prints a greeting to `stdout`. pub fn print_output() { println!("Hello, world!"); println!("Time is: {}", time::now().rfc822()); } /// The program entry point. fn main() { print_output(); }
コードに public 関数 print_output()
が含まれるようになりました。helloworld
プログラム、print_output()
関数、および main()
関数には、ドキュメントコメントが含まれています。
cargo doc
コマンドを実行し、プロジェクトのドキュメンテーションを構築します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo doc --no-deps' Documenting helloworld v0.1.0 (file:///home/vslavik/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.31 secs
Red Hat Enterprise Linux 8 の場合:
$ cargo doc --no-deps Documenting helloworld v0.1.0 (file:///home/vslavik/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.31 secs
インストールされていない場合は、 ツール tree (デフォルトの Red Hat Enterprise Linux リポジトリーで利用できます) をインストールします。
# yum install tree
結果を確認します。
$ tree . ├── Cargo.lock ├── Cargo.toml ├── src │ └── main.rs └── target ... └── doc ... ├── helloworld │ ├── fn.print_output.html │ ├── index.html │ ├── print_output.v.html │ └── sidebar-items.js ... └── src └── helloworld └── main.rs.html 12 directories, 32 files
Cargo はプロジェクトのドキュメンテーションを構築します。ドキュメントを表示するには、ブラウザーで target/doc/helloworld/index.html
ファイルを開きます。生成されたドキュメントには、公開されていないため、main()
関数に関する記述は含まれません。
--no-deps
オプションを指定せずに cargo doc
コマンドを実行し、依存関係ライブラリーの タイム
や libc
を含むプロジェクトドキュメントを構築します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo doc' Documenting libc v0.2.32 Documenting time v0.1.38 Documenting helloworld v0.1.0 (file:///home/vslavik/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 3.41 secs
Red Hat Enterprise Linux 8 の場合:
$ cargo doc Documenting libc v0.2.32 Documenting time v0.1.38 Documenting helloworld v0.1.0 (file:///home/vslavik/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 3.41 secs
tree
コマンドを使用して、生成されたディレクトリー構造を確認します。
$ tree ... 92 directories, 11804 files $ ls -d target/doc/*/ target/doc/helloworld/ target/doc/implementors/ target/doc/libc/ target/doc/src/ target/doc/time/
作成されるドキュメントは、依存関係ライブラリーの time
および libc
にも対応し、それぞれ target/doc/
ディレクトリーに別のサブディレクトリーとして存在します。
関連情報
cargo doc
ツールおよびその機能の詳しい説明は、本書の対象外となっています。詳細は、以下に記載のドキュメントを参照してください。
- 公式 Rust プログラミング言語のドキュメントの 便利なドキュメントコメント
2.8. Vendoring Rust プロジェクトの依存関係
プロジェクト依存関係のベンダーとは、オフライン再分配および再分散のための依存関係のローカルコピーを作成することを意味します。ベンダーの依存関係は、インターネットに接続せずに Cargo ビルドツールで使用することができます。
cargo-vendor
パッケージは Cargo に含まれていますが、動作の方法に変更がないことに注意してください。
例2.8 Vendoring Rust プロジェクトの依存関係
この例では、例2.6「プロジェクトへの依存関係の追加および cargo で構築」 に従って依存関係で Rust プロジェクト helloworld
を正常にビルドしていることを前提としています。
ディレクトリー helloworld
に移動し、cargo vendor
コマンドを実行して依存関係のあるプロジェクトをベンダーします。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo vendor' Downloading kernel32-sys v0.2.2 Downloading redox_syscall v0.1.31 Downloading winapi-build v0.1.1 Downloading winapi v0.2.8 Vendoring kernel32-sys v0.2.2 (/home/vslavik/.cargo/registry/src/github.com-1ecc6299db9ec823/kernel32-sys-0.2.2) to vendor/kernel32-sys Vendoring libc v0.2.32 (/home/vslavik/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.32) to vendor/libc Vendoring redox_syscall v0.1.31 (/home/vslavik/.cargo/registry/src/github.com-1ecc6299db9ec823/redox_syscall-0.1.31) to vendor/redox_syscall Vendoring time v0.1.38 (/home/vslavik/.cargo/registry/src/github.com-1ecc6299db9ec823/time-0.1.38) to vendor/time Vendoring winapi v0.2.8 (/home/vslavik/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-0.2.8) to vendor/winapi Vendoring winapi-build v0.1.1 (/home/vslavik/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-build-0.1.1) to vendor/winapi-build To use vendored sources, add this to your .cargo/config for this project: [source.crates-io] replace-with = "vendored-sources" [source.vendored-sources] directory = "/home/vslavik/helloworld/vendor"
Red Hat Enterprise Linux 8 の場合:
$ cargo vendor
結果を確認します。
$ ls Cargo.lock Cargo.toml src target vendor $ tree vendor vendor ├── kernel32-sys │ ├── build.rs │ ├── Cargo.toml │ ├── README.md │ └── src │ └── lib.rs ├── libc │ ├── appveyor.yml │ ├── Cargo.toml ... 75 directories, 319 files
vendor
ディレクトリーには、helloworld
プログラムを構築するために必要なすべての依存関係 crates のコピーが含まれます。Windows オペレーティングシステムでプロジェクトをビルドするための Crates は、Red Hat Enterprise Linux でこのコマンドを実行してもベンダーが提供されています。
tree ツールは、デフォルトの Red Hat Enterprise Linux リポジトリーから利用できます。インストールするには、以下を行います。
# yum install tree
2.9. 関連情報
Cargo ツールおよびその機能の詳しい説明は、本書の対象外となっています。詳細は、以下に記載のドキュメントを参照してください。
Cargo ドキュメント
cargo(1):
cargo
ツールの man ページは、その使用方法の詳細情報を提供します。Rust ツールセットに含まれるバージョンの man ページを表示するには、次のコマンドを実行します。$ man cargo
Red Hat Enterprise Linux 7 の場合:
$
scl enable rust-toolset-1.41 'man cargo'
Red Hat Enterprise Linux 8 の場合:
$ man cargo
Cargo, Rust's Package Manager HTML ブックは、パッケージとして提供されています。
# yum install cargo-doc
HTML は
usr/share/doc/cargo/html/index.html
で利用できます。Red Hat Enterprise Linux 7 の場合:
# yum install rust-toolset-1.41-cargo-doc
HTML は
/opt/rh/rust-toolset-1.41/root/usr/share/doc/cargo/html/index.html
で利用できます。Red Hat Enterprise Linux 8 の場合:
# yum install cargo-doc
HTML は
/usr/share/doc/cargo/html/index.html
で利用できます。
オンラインの Cargo ドキュメント
以下も併せて参照してください。
- 1章Rust: Rust ツールセットの概要およびそのシステムへのインストール方法の詳細
第3章 rustfmt
rustfmt ツールは、Rust ソースコードの自動フォーマットを提供します。
3.1. rustfmt のインストール
以下のコマンドを実行して rustfmt をインストールします。
Red Hat Enterprise Linux 7 の場合:
# yum install rust-toolset-1.41-rustfmt
Red Hat Enterprise Linux 8 の場合:
# yum install rustfmt
3.2. rustfmt をスタンドアロンツールとして使用
rust ソースファイルとそのすべての依存関係を rustfmt
ツールでフォーマットするには、以下を実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'rustfmt source-file'
Red Hat Enterprise Linux 8 の場合:
$ rustfmt source-file
source-file を、ソースファイルへのパスに置き換えます。
デフォルトでは、rustfmt
は詳細を表示したりバックアップを作成したりせずに、影響を受けるファイルを変更します。動作を変更するには、--write-mode value
オプションを使用します。詳細は、rustfmt
のヘルプメッセージを参照してください。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'rustfmt --help'
Red Hat Enterprise Linux 8 の場合:
$ rustfmt --help
また、rustfmt
はファイルの代わりに標準入力を受け付け、出力を標準出力で提供します。
3.3. cargo での rustfmt の使用
cargo crate のすべてのソースファイルをフォーマットするには、次のコマンドを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'cargo fmt'
Red Hat Enterprise Linux 8 の場合:
$ cargo fmt
rustfmt
フォーマットのオプションを変更するには、プロジェクトディレクトリーに設定ファイル rustfmt.toml
を作成し、そこに設定を指定します。詳細は、rustfmt
のヘルプメッセージを参照してください。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'rustfmt --config-help'
Red Hat Enterprise Linux 8 の場合:
$ rustfmt --config-help
3.4. 関連情報
rustfmt
のヘルプメッセージ:Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.41 'rustfmt --help' $ scl enable rust-toolset-1.41 'rustfmt --config-help'
Red Hat Enterprise Linux 8 の場合:
$ rustfmt --help $ rustfmt --config-help
-
/usr/share/doc/rustfmt/Configurations.md
にある Rustfmt の設定 Configurations.md
での Rustfmt の設定:Red Hat Enterprise Linux 7 の場所:
/opt/rh/rust-toolset-1.41/root/usr/share/doc/rust-toolset-1.41-rustfmt-1.41.0/Configurations.md
Red Hat Enterprise Linux 8 の場所:
/usr/share/doc/rustfmt/Configurations.md
第4章 Rust ツールセットを使用したコンテナーイメージ
Rust ツールセットは、RHEL 7 および RHEL 8 のコンテナーイメージとして利用できます。Red Hat Container Registry からダウンロードできます。
4.1. イメージの内容
RHEL 7 および RHEL 8 コンテナーイメージは、次のパッケージに対応するコンテンツを提供します。
コンポーネント | バージョン | パッケージ |
---|---|---|
| 1.41 |
|
4.2. イメージへのアクセス
イメージをプルするには、root で以下のコマンドを実行します。
# podman pull registry.redhat.io/devtools/rust-toolset-rhel7
# podman pull registry.redhat.io/rhel8/rust-toolset
4.3. Source-to-Image を使用したビルダーイメージとしての使用
RUST ツールセットコンテナーイメージは、Source-to-Image(S2I) ビルダーイメージとして使用するために準備されます。
例4.1 Source-to-Image を使用した Rust アプリケーションイメージの構築
GitHub リポジトリーから rust-test-app を構築するには、以下の手順を実行します。
前提条件
- 以下のコマンドを実行してビルダーイメージを準備します。
$ git clone https://github.com/openshift-s2i/s2i-rust.git $ cd s2i-rust $ make build
手順
s2i
ツールを使用して、以下のコマンドで rust-test-app をビルドします。
s2i build ./examples/test-app/ devtools/rust-toolset-rhel7 rust-test-app
イメージを実行するには、コマンドラインに以下のコマンドを入力します。
$ docker run rust-test-app
ローカルで利用可能なアプリケーションイメージ rust-test-app は、rust-toolset コンテナーイメージを使用してビルドされます。
Rust を S2I ビルダーイメージとしてフルに活用するには、変更した S2I assemble スクリプトを使用し、ビルドしている特定のアプリケーションをさらに調整してカスタムイメージをビルドします。
Source-to-Image を使用した Rust の使用方法の詳細な説明は、本書の対象外です。Source-to-Image の詳細は以下を参照してください。
- OpenShift Container Platform 4.5 Image Creation Guide、OpenShift Container Platform-specific guidelines
- Using Red Hat Software Collections Container Images、第 2 章Source-to-Image (S2I) の使用
4.4. 関連情報
- Rust 1.41 コンテナーイメージ: Red Hat Container Registry のエントリー
- Red Hat Software Collections コンテナーイメージの使用
第5章 Rust 1.41 Toolset の変更点
Rust ツールセット のバージョンが 1.39 から 1.41 に更新されました。以下は、主な変更点です。
- 孤立したルールは厳密性が低くなったので、新規特性の実装が簡素化されました。
-
これで、
#[non_exhaustive]
属性をstruct
、enum
またはenum
バリアントにアタッチできるようになりました。 -
Foreign Function Interface (FFI) で
Box<T>
を使用すると、より多くの保証が提供されます。Box<T>
には、FFI のT*
ポインターと同じ Application Binary Interface (ABI) があります。 - rust はコンパイル時にメモリーの安全性のバグを検出しますが、以前のボローチェッカーには制限がありました。そのため、未定義の動作が許可され、メモリーの危険性が発生していました。新しい NLL (Non-lexical lifetimes) ボローチェッカーは、メモリーの安全性の問題を、ハードエラーとして報告する可能性があります。これは、Rust 2015 および Rust 2018 エディションに適用されるようになりました。以前のリリースでは、Rust 2015 は、NLL ボローチェッカーは、このような問題に関する警告のみを表示していました。