Skip to content

[2024] #94 changes to the prelude を翻訳 #120

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 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
- [`gen` keyword](rust-2024/gen-keyword.md)
- [Reserved syntax](rust-2024/reserved-syntax.md)
- [標準ライブラリ](rust-2024/standard-library.md)
- [Changes to the prelude](rust-2024/prelude.md)
- [Prelude への変更点](rust-2024/prelude.md)
- [Add `IntoIterator` for `Box<[T]>`](rust-2024/intoiterator-box-slice.md)
- [Newly unsafe functions](rust-2024/newly-unsafe-functions.md)
- [Cargo](rust-2024/cargo.md)
Expand Down
124 changes: 105 additions & 19 deletions src/rust-2024/prelude.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,88 @@
> **Rust Edition Guide は現在 Rust 2024 のアップデート作業に向けて翻訳作業中です。本ページはある時点での英語版をコピーしていますが、一部のリンクが動作しない場合や、最新情報が更新されていない場合があります。問題が発生した場合は、[原文(英語版)](https://doc.rust-lang.org/nightly/edition-guide/introduction.html)をご参照ください。**
<!--
# Changes to the prelude
-->

# Changes to the prelude
# Prelude への変更点

## Summary
<!--
## Summary
-->

## 概要

<!--
- The [`Future`] and [`IntoFuture`] traits are now part of the prelude.
- This might make calls to trait methods ambiguous which could make some code fail to compile.
- This might make calls to trait methods ambiguous which could make some code fail to compile.
-->

- [`Future`] と [`IntoFuture`] トレイトがプレリュードに追加されました。
- その結果、一部のコードでトレイトメソッドの呼び出しが一意に決定できなくなり、コンパイルに失敗する可能性があります。

[`Future`]: ../../std/future/trait.Future.html
[`IntoFuture`]: ../../std/future/trait.IntoFuture.html
[`Future`]: https://doc.rust-lang.org/std/future/trait.Future.html
[`IntoFuture`]: https://doc.rust-lang.org/std/future/trait.IntoFuture.html

## Details
<!--
## Details
-->

## 詳細

<!--
The [prelude of the standard library](../../std/prelude/index.html) is the module containing everything that is automatically imported in every module.
It contains commonly used items such as `Option`, `Vec`, `drop`, and `Clone`.
It contains commonly used items such as `Option`, `Vec`, `drop`, and `Clone`.
-->

[標準ライブラリのプレリュード](https://doc.rust-lang.org/std/prelude/index.html) は、すべてのモジュールで自動的にインポートされる項目をまとめたモジュールです。例えば、`Option`、`Vec`、`drop`、`Clone` など、よく使われる項目が含まれます。


<!--
The Rust compiler prioritizes any manually imported items over those from the prelude,
to make sure additions to the prelude will not break any existing code.
For example, if you have a crate or module called `example` containing a `pub struct Option;`,
then `use example::*;` will make `Option` unambiguously refer to the one from `example`;
not the one from the standard library.
not the one from the standard library.
-->

Rust コンパイラは、Prelude からインポートされる項目よりも、明示的にインポートされた項目を優先します。たとえば、`example` というクレートやモジュールに `pub struct Option;` が定義されている場合、`use example::*;` とした場合、`Option` は標準ライブラリのものではなく `example` のものが使われます。

<!--
However, adding a _trait_ to the prelude can break existing code in a subtle way.
For example, a call to `x.poll()` which comes from a `MyPoller` trait might fail to compile if `std`'s `Future` is also imported, because the call to `poll` is now ambiguous and could come from either trait.
For example, a call to `x.poll()` which comes from a `MyPoller` trait might fail to compile if `std`'s `Future` is also imported, because the call to `poll` is now ambiguous and could come from either trait.
-->

ただし、_トレイト_ をプレリュードに追加すると、既存のコードに微妙な影響が生じ壊れることがあります。たとえば、`MyPoller` というトレイトの `poll` メソッドを `x.poll()` と呼び出しているコードに、`std` (標準ライブラリ) の `Future` トレイトも同時にインポートされている場合、どちらの `poll` を呼ぶべきかが一意に決定できなくなり、コンパイルに失敗する可能性があります。

<!--
As a solution, Rust 2024 will use a new prelude.
It's identical to the current one, except for the following changes:
It's identical to the current one, except for the following changes:
-->

- Added:
そのため、Rust 2024 では新しいプレリュードが導入されます。基本的な内容は現行のものと変わりませんが、以下の項目が追加されています:

<!--
- Added:
-->
- 追加された項目:
- [`std::future::Future`][`Future`]
- [`std::future::IntoFuture`][`IntoFuture`]

## Migration
<!--
## Migration
-->

## 移行

<!--
### Conflicting trait methods
-->

### Conflicting trait methods
### 競合するトレイトメソッド

When two traits that are in scope have the same method name, it is ambiguous which trait method should be used. For example:
<!--
When two traits that are in scope have the same method name, it is ambiguous which trait method should be used. For example:
-->

スコープ内にある二つのトレイトが同じメソッド名を持つ場合、どちらのトレイトメソッドを使うべきかが一意に決定できなくなってしまいます。例えば、次のコードをご覧ください。

```rust,edition2021
trait MyPoller {
Expand All @@ -55,7 +102,11 @@ fn main() {
}
```

We can fix this so that it works on all editions by using fully qualified syntax:
<!--
We can fix this so that it works on all editions by using fully qualified syntax:
-->

これをすべてのエディションで正しく動作させるためには、完全修飾構文を使う必要があります。

```rust,ignore
fn main() {
Expand All @@ -64,17 +115,52 @@ fn main() {
}
```

The [`rust_2024_prelude_collisions`] lint will automatically modify any ambiguous method calls to use fully qualified syntax. This lint is part of the `rust-2024-compatibility` lint group, which will automatically be applied when running `cargo fix --edition`. To migrate your code to be Rust 2024 Edition compatible, run:

<!--
The [`rust_2024_prelude_collisions`] lint will automatically modify any ambiguous method calls to use fully qualified syntax. This lint is part of the `rust-2024-compatibility` lint group, which will automatically be applied when running `cargo fix --edition`. To migrate your code to be Rust 2024 Edition compatible, run:
-->

また、[`rust_2024_prelude_collisions`] リントは、一意に決定できない曖昧なメソッド呼び出しを自動的に完全修飾構文に変更してくれます。このリントは `rust-2024-compatibility` リントグループの一部であり、`cargo fix --edition` を実行すると自動的に適用されます。Rust 2024 エディションに互換性のあるコードに移行するには、次のコマンドを実行してください。

```sh
cargo fix --edition
```

Alternatively, you can manually enable the lint to find places where these qualifications need to be added:
<!--
Alternatively, you can manually enable the lint to find places where these qualifications need to be added:
-->

もしくは、完全修飾が必要な箇所を見つけるために、リントを手動で有効にすることもできます。

```rust
// Add this to the root of your crate to do a manual migration.
#![warn(rust_2024_prelude_collisions)]
```

[`rust_2024_prelude_collisions`]: ../../rustc/lints/listing/allowed-by-default.html#rust-2024-prelude-collisions
<!--
### `RustcEncodable` and `RustcDecodable`
-->

### `RustcEncodable` と `RustcDecodable`

<!--
It is strongly recommended that you migrate to a different serialization library if you are still using these.
However, these derive macros are still available in the standard library, they are just required to be imported from the older prelude now:
-->

これらのシリアライズライブラリをまだ使用している場合は、別のライブラリへの移行を強く推奨します。
しかし、これらの derive マクロは依然として標準ライブラリに含まれており、古い Prelude からインポートする必要があります:

```rust,edition2021
#[allow(soft_unstable)]
use core::prelude::v1::{RustcDecodable, RustcEncodable};
```

<!--
There is no automatic migration for this change; you will need to make the update manually.
-->

この変更に対する 自動移行手段はありません。そのため、手動で更新を行う必要があります。


[`rust_2024_prelude_collisions`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#rust-2024-prelude-collisions