Skip to content

[2024] #107 add into iterator boxを翻訳 #121

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

<!--
# Add `IntoIterator` for `Box<[T]>`
-->

# Box<[T]> に対する IntoIterator の追加

<!--
## Summary
-->

## 概要

<!--
- Boxed slices implement [`IntoIterator`] in *all* editions.
- Calls to [`IntoIterator::into_iter`] are *hidden* in editions prior to 2024 when using method call syntax (i.e., `boxed_slice.into_iter()`). So, `boxed_slice.into_iter()` still resolves to `(&(*boxed_slice)).into_iter()` as it has before.
- `boxed_slice.into_iter()` changes meaning to call [`IntoIterator::into_iter`] in Rust 2024.
-->

- ボックス化されたスライスは、*すべての*エディションで [`IntoIterator`] を実装します。
- 2024年以前のエディションでは、メソッド呼び出し構文(例: `boxed_slice.into_iter()` )で [`IntoIterator::into_iter`] への呼び出しが*隠蔽*されるため、これまでどおり `boxed_slice.into_iter()` は `(&(*boxed_slice)).into_iter()` として解釈されます。
- Rust 2024 では、`boxed_slice.into_iter()` の意味が `IntoIterator::into_iter` を呼び出すものに変わります。

<!--
[`IntoIterator`]: ../../std/iter/trait.IntoIterator.html
[`IntoIterator::into_iter`]: ../../std/iter/trait.IntoIterator.html#tymethod.into_iter
-->

[`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html
[`IntoIterator::into_iter`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html#tymethod.into_iter

<!--
## Details
-->

## 詳細

<!--
Until Rust 1.80, `IntoIterator` was not implemented for boxed slices. In prior versions, if you called `.into_iter()` on a boxed slice, the method call would automatically dereference from `Box<[T]>` to `&[T]`, and return an iterator that yielded references of `&T`. For example, the following worked in prior versions:
-->

Rust 1.80 以前は、ボックス化されたスライスに対して `IntoIterator` が実装されていませんでした。以前のバージョンでは、ボックス化されたスライスに対して `.into_iter()` を呼び出すと、メソッド呼び出しが自動的に `Box<[T]>` から `&[T]` への参照外しを行い、`&T` の参照を返すイテレータが生成されました。たとえば、以下のコードは以前のエディションで動作していました:

<!--
```rust
// Example of behavior in previous editions.
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
Expand All @@ -23,21 +49,54 @@ for x in my_boxed_slice.into_iter() {
// x is of type &u32 in editions prior to 2024
}
```
-->

```rust
// 以前のエディションでの動作例
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
// 注意:1.80以前のバージョンでは .into_iter() の呼び出しが必要です
for x in my_boxed_slice.into_iter() {
// Rust 2024 以前のエディションでは、x の型は &u32 です
}
```

<!--
In Rust 1.80, implementations of `IntoIterator` were added for boxed slices. This allows iterating over elements of the slice by-value instead of by-reference:
-->

Rust 1.80 では、ボックス化されたスライスに対して `IntoIterator` の実装が追加されました。これにより、スライスの要素を参照ではなく値としてイテレートできるようになります:

<!--
```rust
// NEW as of 1.80, all editions
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
for x in my_boxed_slice { // notice no need for calling .into_iter()
// x is of type u32
}
```
-->

```rust
// 1.80以降、すべてのエディションでの新しい動作
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
for x in my_boxed_slice { // .into_iter() の呼び出しは不要になっています
// x の型は u32 です
}
```

<!--
This example is allowed on all editions because previously this was an error since `for` loops do not automatically dereference like the `.into_iter()` method call does.
-->

この例は、すべてのエディションで許可されています。というのも、以前は `for` ループが `.into_iter()` のように自動的に参照外しを行わなかったため、そもそもエラーになっていたからです。

<!--
However, this would normally be a breaking change because existing code that manually called `.into_iter()` on a boxed slice would change from having an iterator over references to an iterator over values. To resolve this problem, method calls of `.into_iter()` on boxed slices have edition-dependent behavior. In editions before 2024, it continues to return an iterator over references, and starting in Edition 2024 it returns an iterator over values.
-->

しかし、本来であれば、これは破壊的変更となる可能性があります。なぜなら、これまで `.into_iter()` を明示的に呼び出していたコードの動作が、参照を返すイテレータから値を返すイテレータへと変わってしまうからです。この問題を解決するために、ボックス化されたスライスの `.into_iter()` の動作はエディションによって異なります。2024年以前のエディションでは、これまでどおり参照を返すイテレータを生成します。Rust 2024 以降では、値を返すイテレータを生成します。

<!--
```rust,edition2024
// Example of changed behavior in Edition 2024
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
Expand All @@ -46,17 +105,41 @@ for x in my_boxed_slice.into_iter() {
// x is now type u32 in Edition 2024
}
```
-->

```rust,edition2024
// Edition 2024 での動作変更の例
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
// Example of old code that still manually calls .into_iter()
// .into_iter() を明示的に呼び出していた古いコードの例
for x in my_boxed_slice.into_iter() {
// Edition 2024 では、x の型は u32 になります
}
```

<!--
## Migration
-->

## 移行

<!--
The [`boxed_slice_into_iter`] lint will automatically modify any calls to `.into_iter()` on boxed slices to call `.iter()` instead to retain the old behavior of yielding references. 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:
-->

[`boxed_slice_into_iter`] リントは、ボックス化されたスライスに対する `.into_iter()` の呼び出しを `.iter()` に自動で置き換え、従来どおり参照を返すように修正します。このリントは `rust-2024-compatibility` リントグループの一部であり、`cargo fix --edition` を実行すると自動的に適用されます。Rust 2024 エディションに対応するために、次のコマンドを実行してください。

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

<!--
For example, this will change:
-->

例えば、以下のコードは:

<!--
```rust
fn main() {
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
Expand All @@ -65,9 +148,24 @@ fn main() {
}
}
```
-->

```rust
fn main() {
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
for x in my_boxed_slice.into_iter() {
// x の型は &u32
}
}
```

<!--
to be:
-->

次のように修正されます:

<!--
```rust
fn main() {
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
Expand All @@ -76,7 +174,25 @@ fn main() {
}
}
```
-->

```rust
fn main() {
let my_boxed_slice: Box<[u32]> = vec![1, 2, 3].into_boxed_slice();
for x in my_boxed_slice.iter() {
// x の型は &u32
}
}
```

<!--
The [`boxed_slice_into_iter`] lint is defaulted to warn on all editions, so unless you have manually silenced the lint, you should already see it before you migrate.
-->

[`boxed_slice_into_iter`] リントはすべてのエディションでデフォルトで警告を出す設定になっているため、手動でリントを無効化していなければ、移行前にすでに警告が表示されるはずです。

<!--
[`boxed_slice_into_iter`]: ../../rustc/lints/listing/warn-by-default.html#boxed-slice-into-iter
-->

[`boxed_slice_into_iter`]: https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#boxed-slice-into-iter