Skip to content
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

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

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
108 changes: 106 additions & 2 deletions src/rust-2024/prelude.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,95 @@
> **Rust Edition Guide は現在 Rust 2024 のアップデート作業に向けて翻訳作業中です。本ページはある時点での英語版をコピーしていますが、一部のリンクが動作しない場合や、最新情報が更新されていない場合があります。問題が発生した場合は、[原文(英語版)](https://doc.rust-lang.org/nightly/edition-guide/introduction.html)をご参照ください。**

<!--
# Changes to the prelude
-->

# Prelude への変更点

<!--
## 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.
-->

- [`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
-->

## 詳細

<!--
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`.
-->

[標準ライブラリのプレリュード](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.
-->

Rust コンパイラは、プレリュードからインポートされるアイテムよりも、明示的にインポートされたアイテムを優先します。これにより、プレリュードに追加があっても既存のコードは壊れないようになっています。たとえば、`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.
-->

ただし、*トレイト*をプレリュードに追加すると、既存のコードに微妙な影響が生じて壊れることがあります。たとえば、`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:
-->

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

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

<!--
## Migration
-->

## 移行

<!--
### 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:
-->

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

<!--
```rust,edition2021
trait MyPoller {
// This name is the same as the `poll` method on the `Future` trait from `std`.
Expand All @@ -54,27 +107,78 @@ fn main() {
core::pin::pin!(async {}).poll();
}
```
-->

```rust,edition2021
trait MyPoller {
// この名前は、`std` の `Future` トレイトにある `poll` メソッドと同じです。
fn poll(&self) {
println!("polling");
}
}

impl<T> MyPoller for T {}

fn main() {
// `Pin<&mut async {}>` は `std::future::Future` と `MyPoller` の両方を実装しています。
// 両方のトレイトがスコープ内にある場合(Rust 2024 ではこれが発生する)、
// どの `poll` メソッドを呼び出すかが曖昧になります。
core::pin::pin!(async {}).poll();
}
```

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

完全修飾構文を使うと、すべてのエディションで正しく動作するようになります。

<!--
```rust,ignore
fn main() {
// Now it is clear which trait method we're referring to
<_ as MyPoller>::poll(&core::pin::pin!(async {}));
}
```
-->

```rust,ignore
fn main() {
// これで、どのトレイトのメソッドを参照しているのかが明確になりました。
<_ as MyPoller>::poll(&core::pin::pin!(async {}));
}
```

<!--
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:
-->

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

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

```rust
// 手動で移行を行うには、クレートのルートにこれを追加してください。
#![warn(rust_2024_prelude_collisions)]
```

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

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