Skip to content

「既存のプロジェクトのエディションを移行する」を更新 #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 152 additions & 20 deletions src/editions/transitioning-an-existing-project-to-a-new-edition.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!--
# Transitioning an existing project to a new edition
-->

# 既存のプロジェクトのエディションを移行する

<!--
Expand All @@ -9,19 +10,23 @@ It will update your source code so that it is compatible with the next edition.
Briefly, the steps to update to the next edition are:
-->

Rust には、プロジェクトのエディションを進めるための自動移行ツールが付属しています
このツールは、あなたのソースコードを書き換えて次のエディションに適合させます
簡単にいうと、新しいエディションに進むためには次のようにすればよいです。
Rust には、プロジェクトのエディションを更新するための自動移行ツールが付属しています
このツールは、ソースコードを書き換えて次のエディションに適合させます
新エディションを採用するために必要な手順は、概ね以下の通りです:

<!--
1. Run `cargo fix --edition`
2. Edit `Cargo.toml` and set the `edition` field to the next edition, for example `edition = "2021"`
3. Run `cargo build` or `cargo test` to verify the fixes worked.
1. Run `cargo update` to update your dependencies to the latest versions.
2. Run `cargo fix --edition`
3. Edit `Cargo.toml` and set the `edition` field to the next edition, for example `edition = "2024"`
4. Run `cargo build` or `cargo test` to verify the fixes worked.
5. Run `cargo fmt` to reformat your project.
-->

1. `cargo fix --edition` を実行する
2. `Cargo.toml` の `edition` フィールドを新しいエディションに設定する。たとえば、 `edition = "2021"` とする
3. `cargo build` や `cargo test` を実行して、修正がうまくいったことを検証する。
1. `cargo update` を実行して、依存ライブラリを最新版にする
2. `cargo fix --edition` を実行する
3. `Cargo.toml` の `edition` フィールドを新しいエディションに設定する。たとえば、 `edition = "2024"` とする
4. `cargo build` や `cargo test` を実行して、修正がうまくいったことを検証する
5. `cargo fmt` を実行して、コードをリフォーマットする

<!--
The following sections dig into the details of these steps, and some of the issues you may encounter along the way.
Expand All @@ -42,7 +47,7 @@ The following sections dig into the details of these steps, and some of the issu
> よろしくお願いします!
>
> 訳注:Rustの日本語コミュニティもあります。
> Slackを使用しており[こちら](https://rust-jp.herokuapp.com/)から登録できます。
> Zulip を使用しており、[こちら](https://rust-lang-jp.zulipchat.com/)から登録できます。

<!--
## Starting the migration
Expand Down Expand Up @@ -80,20 +85,45 @@ so this would fail to compile. Let's get this code up to date!
これは [Rust 2018ではサポートされておらず](../rust-2018/trait-system/no-anon-params.md)、コンパイルに失敗します。
このコードを更新してみましょう。

<!--
## Updating your dependencies
-->

## 依存ライブラリを更新する

<!--
Before we get started, it is recommended to update your dependencies. Some dependencies, particularly some proc-macros or dependencies that do build-time code generation, may have compatibility issues with newer editions. New releases may have been made since you last updated which may fix these issues. Run the following:
-->

作業前に、依存ライブラリを更新することをお勧めします。特に、proc macro や、ビルド時にコードを生成するような依存ライブラリは、エディションを進めることで互換性の問題が生じる場合があります。
依存ライブラリを更新をしていなかった間に、それらの問題を解消した新バージョンが出ているかもしれません。
以下のコマンドで依存ライブラリを更新できます。

```console
cargo update
```

<!--
After updating, you may want to run your tests to verify everything is working. If you are using a source control tool such as `git`, you may want to commit these changes separately to keep a logical separation of commits.
-->

更新後、コードが壊れていないことを確認するため、テストを実行しておくとよいでしょう。
`git` などのバージョン管理システムを使っているなら、適切なコミット粒度のためにここで一旦コミットしておくとよいでしょう。

<!--
## Updating your code to be compatible with the new edition
-->

## あなたのコードを新しいエディションでコンパイルできるようにする
## コードが新しいエディションでコンパイルできるようにする

<!--
Your code may or may not use features that are incompatible with the new edition.
In order to help transition to the next edition, Cargo includes the [`cargo fix`] subcommand to automatically update your source code.
To start, let's run it:
-->

あなたのコードは新しいエディションに互換性のない機能を使っているかもしれないし、使っていないかもしれません
Cargo には [`cargo fix`] というサブコマンドがあり、これがあなたのコードを自動的に更新して次のエディションへの移行を補助してくれます
コードは、新しいエディションと非互換な機能を使っている可能性があります
Cargo [`cargo fix`] サブコマンドで、コードを自動修正して次のエディションへ移行につなげることができます
まず初めに、これを実行してみましょう。

```console
Expand All @@ -105,7 +135,7 @@ This will check your code, and automatically fix any issues that it can.
Let's look at `src/lib.rs` again:
-->

これはあなたのコードをチェックして、自動的に移行の問題を修正してくれます
コードは検査され、移行で発生する問題が自動的に修正されます
もう一度 `src/lib.rs`を見てみましょう。

```rust
Expand All @@ -121,7 +151,7 @@ which is conventional for unused variables.
-->

`i32` 値をとるパラメータに名前が追加された形でコードが書き換えられています。
この場合は、パラメータ名がなかったので、使用されていないパラメータの慣習に従って `_` を付加しています。
この場合は、パラメータ名がなかったので、未使用パラメータの慣習に従って `_` を付加しています。

<!--
`cargo fix` can't always fix your code automatically.
Expand All @@ -132,9 +162,10 @@ If you have problems, please seek help at the [user's forums](https://users.rust
-->

`cargo fix`は常に自動的にコードを修正してくれるわけではありません。
もし、`cargo fix`がコードを修正できない時にはコンソールに修正できなかったという警告を表示します。
その場合は手動でコードを修正してください。
「[発展的な移行戦略]」<!-- TODO: 章の名前に合わせてリンク名を変える必要があるかもしれません -->の章では、移行に関するより多くの情報があります。また、このガイドの他の章では、どのような変更が必要かについても説明しますので、併せてご参照ください。
`cargo fix` がコードを修正できない場合、コンソールに修正できなかったという警告を表示します。
そのときは手動でコードを修正してください。
「[発展的な移行戦略]」の節には、移行に関する詳細情報があります。
また他の節では、どのような変更が必要かについても説明していますので、併せてご参照ください。
問題が発生したときは、[ユーザーフォーラム](https://users.rust-lang.org/) で助けを求めてください。

<!--
Expand Down Expand Up @@ -169,30 +200,131 @@ we've chosen `2018`, and so our code will compile with Rust 2018!
もし `edition` キーがなければCargoはデフォルトで Rust 2015をエディションとして使います。
しかし、上記の例では `2018` を明示的に指定しているので、コードは Rust 2018 でコンパイルされます!

<!--
## Testing your code in the new edition
-->

## 新エディションでテストを実行する

<!--
The next step is to test your project on the new edition.
Run your project tests to verify that everything still works, such as running [`cargo test`].
If new warnings are issued, you may want to consider running `cargo fix` again (without the `--edition` flag) to apply any suggestions given by the compiler.
-->

次に、新しいエディション上であなたのプロジェクトをテストしましょう
次に、新しいエディション上でプロジェクトのテストを実行しましょう
[`cargo test`] を実行するなどして、プロジェクトのテストを走らせ、すべてが元のまま動くことを確認してください。
新たに警告が出た場合、(`--edition` なしの) `cargo fix` をもう一度実行することで、コンパイラからの提案を受け入れてみるのも良いかもしれません。

<!--
At this point, you may still need to do some manual changes. For example, the automatic migration does not update doctests, and build-time code generation or macros may need manual updating. See the [advanced migrations chapter] for more information.
-->

ここでも手動での変更が必要となる場合があります。たとえば、自動移行では doctest は修正されませんし、ビルド時コード生成やマクロなどは自分で書き換える必要があるかもしれません。
詳しくは「[発展的な移行戦略]」の節をご参照ください。

<!--
Congrats! Your code is now valid in both Rust 2015 and Rust 2018!
-->

わーい。今やあなたのコードは Rust 2015 と Rust 2018 の両方で有効です!

<!--
[advanced migrations chapter]: advanced-migrations.md
-->

<!--
## Reformatting with rustfmt
-->

## Rustfmt を用いてフォーマットする

<!--
If you use [rustfmt] to automatically maintain formatting within your project, then you should consider reformatting using the new formatting rules of the new edition.
-->

コードのフォーマットに [rustfmt] を利用しているなら、新エディションの新しいフォーマット規則に従ってフォーマットを実行すべきでしょう。

<!--
Before reformatting, if you are using a source control tool such as `git`, you may want to commit all the changes you have made up to this point before taking this step. It can be useful to put formatting changes in a separate commit, because then you can see which changes are just formatting versus other code changes, and also possibly ignore the formatting changes in `git blame`.
-->

`git` などのバージョン管理システムを使っているなら、フォーマット実行前に現時点で一旦コミットしておくとよいでしょう。
フォーマットの変更を別のコミットに分けておくと、コード整形とコードの書き換えを見分けたり、`git blame` でフォーマットの変更を無視したりと、後々役に立つかもしれません。

```console
cargo fmt
```

<!--
See the [style editions chapter] for more information.
-->

詳細は、[rustfmt の変更] に関する節をご参照ください。

<!--
[rustfmt]: https://github.com/rust-lang/rustfmt
[style editions chapter]: ../rust-2024/rustfmt-style-edition.md
-->

[rustfmt]: https://github.com/rust-lang/rustfmt
[rustfmt の変更]: ../rust-2024/rustfmt-style-edition.md

<!--
## Migrating to an unstable edition
-->

## 安定化されていないエディションに移行する

<!--
After an edition is released, there is roughly a three year window before the next edition.
During that window, new features may be added to the next edition, which will only be available on the [nightly channel].
If you want to help test those new features before they are stabilized, you can use the nightly channel to try them out.
-->

エディションのリリース後、次のエディションが出るまでざっと3年かかります。
それまでに、新機能が次期エディションに追加されることがあります。このエディションは [nightly チャンネル]限定です。
新機能の安定化前の試用に協力したい場合、nightly チャンネルを使うとよいです。

<!--
The steps are roughly similar to the stable channel:
-->

基本的には手順は stable チャンネルと同様です。

<!--
1. Install the most recent nightly: `rustup update nightly`.
2. Run `cargo +nightly fix --edition`.
3. Edit `Cargo.toml` and place `cargo-features = ["edition20xx"]` at the top (above `[package]`), and change the edition field to say `edition = "20xx"` where `20xx` is the edition you are upgrading to.
4. Run `cargo +nightly check` to verify it now works in the new edition.
-->

1. `rustup update nightly` で最新の nightly をインストールします。
2. `cargo +nightly fix --edition` を実行します。
3. `Cargo.toml` を編集して、`cargo-features = ["edition20xx"]` を最上部に(`[package]` より前に)記述して、`edition = "20xx"` のように edition フィールドを書き換えます(`20xx` は移行先のエディション)。
4. `cargo +nightly check` を実行して、新エディションでも問題ないことを確認します。

<!--
> **⚠ Caution**: Features implemented in the next edition may not have automatic migrations implemented with `cargo fix`, and the features themselves may not be finished.
> When possible, this guide should contain information about which features are implemented
> on nightly along with more information about their status.
> A few months before the edition is stabilized, all of the new features should be fully implemented, and the [Rust Blog] will announce a call for testing.
-->

> **⚠ 注意**: 次期エディションに実装された機能は、ものによっては `cargo fix` による自動移行が使えなかったり、機能自体が未完成だったりします。
> 本ガイドでは、nightly に実装されている機能の十分な情報や開発状況を可能な限り説明します。
> エディションが安定化される数ヶ月前までには、全ての新機能が完全に実装され、[Rust Blog] で試用が呼びかけられます。

<!--
[`cargo fix`]: ../../cargo/commands/cargo-fix.html
[`cargo test`]: ../../cargo/commands/cargo-test.html
[Advanced migration strategies]: advanced-migrations.md
[nightly channel]: ../../book/appendix-07-nightly-rust.html
[Rust Blog]: https://blog.rust-lang.org/
-->

[`cargo fix`]: ../../cargo/commands/cargo-fix.html
[`cargo test`]: ../../cargo/commands/cargo-test.html
[発展的な移行戦略]: advanced-migrations.md
[nightly channel]: ../../book/appendix-07-nightly-rust.html
[nightly チャンネル]: ../../book/appendix-07-nightly-rust.html
[Rust Blog]: https://blog.rust-lang.org/