|
| 1 | +<!-- |
1 | 2 | # Cargo changes |
| 3 | +--> |
2 | 4 |
|
| 5 | +# Cargo への変更 |
| 6 | + |
| 7 | +<!-- |
3 | 8 | ## Summary |
| 9 | +--> |
| 10 | + |
| 11 | +## 概要 |
4 | 12 |
|
| 13 | +<!-- |
5 | 14 | - If there is a target definition in a `Cargo.toml` manifest, it no longer |
6 | 15 | automatically disables automatic discovery of other targets. |
7 | 16 | - Target paths of the form `src/{target_name}.rs` are no longer inferred for |
8 | 17 | targets where the `path` field is not set. |
9 | 18 | - `cargo install` for the current directory is no longer allowed, you must |
10 | | - specify `cargo install --path .` to install the current package. |
| 19 | + specify `cargo install --path .` to install the current package. |
| 20 | +--> |
| 21 | + |
| 22 | +- `Cargo.toml` にターゲットの指定がある場合であっても、他のターゲットの自動探索がされなくなるということはなくなりました[^1]。 |
| 23 | +- `path` フィールドが指定されていないターゲットに対して、`src/{target_name}.rs` の形のターゲットパスは自動推定されなくなりました[^2]。 |
| 24 | +- 現在のディレクトリに対して `cargo install` できなくなりました。現在のパッケージをインストールしたい場合は `cargo install --path .` と指定する必要があります[^3]。 |
| 25 | + |
| 26 | +[^1] *訳注*: |
| 27 | + Cargo は、`Cargo.toml` 内に明示的に指定されていなくても、[フォルダ構成の慣習][package-layout]に従っているファイルに関しては、自動でターゲットに追加します。 |
| 28 | + 例えば、`src/bin/my_application.rs` というファイルがプロジェクト内に存在したら、`Cargo.toml` に `[[bin]]` セクションで `my_application` の存在が宣言されていなくても、自動的にこのファイルがバイナリターゲット(つまり、`cargo run --bin my_application` として実行できるもの)としてビルドされるようになっています。 |
| 29 | + これは、[ターゲットの自動探索][target-auto-discovery]と呼ばれています。<br> |
| 30 | + Rust 2015 と Rust 2018 の違いは、`Cargo.toml` に明示的にターゲットが1つ以上宣言されている場合(つまり、`[lib]`, `[[bin]]`, `[[test]]`, `[[bench]]`, `[[example]]` のどれかが1つ以上ある場合)に生じます。 |
| 31 | + Rust 2015 では、これらのうちどれか1つが指定されていた場合、ターゲットの自動探索は無効化されました。 |
| 32 | + Rust 2018 では、その場合であっても、ターゲットの自動探索は無効化されません。<br> |
| 33 | + 詳細は、[Cargo Book の "Target auto-discovery"(英語)][target-auto-discovery]もご覧ください。 |
| 34 | + |
| 35 | +[^2] *訳注*: |
| 36 | + Cargo は、ターゲットを宣言するセクション(つまり、例えば `[lib]` セクションや `[[bin]]` セクション)に `path` フィールドがなかった場合、 |
| 37 | + `name` フィールドと[フォルダ構成の慣習][package-layout]に従ってパスを推論し、そのパスが存在すればそれを使用します。 |
| 38 | + たとえば、`Cargo.toml`に |
| 39 | + ```toml |
| 40 | + [[bin]] |
| 41 | + name = "my_binary" |
| 42 | + ``` |
| 43 | + というセクションがあった場合、`path` フィールドが `src/bin/my_binary.rs` であると自動的に判断します。 |
| 44 | + そして、`src/bin/my_binary.rs` が存在すれば、これをターゲットのソースとして採用することになります。 |
| 45 | + Rust 2015 では、これに加えて `path` が `src/my_binary.rs` である可能性も候補になりました。 |
| 46 | + すなわち、上の状況において `src/bin/my_binary.rs` が存在しないが `src/my_binary.rs` が存在する場合は、`src/my_binary.rs` をターゲットのソースとして採用しました。 |
| 47 | + しかし、この挙動は時にわかりにくいエラーを生むため、この形のパスが候補とされる挙動を利用することは[非推奨となり](https://github.com/rust-lang/cargo/pull/4259#issuecomment-314138552)、Rust 2018 では挙動そのものが廃止されました。 |
| 48 | + |
| 49 | +[^3] *訳注*: |
| 50 | + [`cargo install`][cargo-install] コマンドは、指定したクレートのバイナリターゲットをインストールする、つまりビルドして実行ファイルを所定の場所に配置するためのコマンドです。 |
| 51 | + Rust 2015 では、カレントディレクトリが Cargo プロジェクト下であったときに `cargo install` とだけ実行すると、そのプロジェクトに含まれるクレートを対象としてインストールが実行されました。 |
| 52 | + Rust 2018 ではこの挙動は廃止され、`cargo install --path .` と明示的にカレントディレクトリのクレートをインストールの対象にすると宣言しなければならなくなりました。 |
| 53 | + |
| 54 | +[package-layout]: https://doc.rust-lang.org/cargo/guide/project-layout.html |
| 55 | +[target-auto-discovery]: https://doc.rust-lang.org/cargo/reference/cargo-targets.html#target-auto-discovery |
| 56 | +[cargo-install]: https://doc.rust-lang.org/cargo/commands/cargo-install.html |
0 commit comments