Skip to content

Commit 0d6b4e0

Browse files
authored
Merge pull request #65 from TonalidadeHidrica/new-keywords
「新しいキーワード」を翻訳
2 parents fbed01d + d2a683c commit 0d6b4e0

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

TranslationTable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
| standard library | 標準ライブラリ
215215
| statement | 文
216216
| static dispatch | 静的ディスパッチ
217+
| strict keyword | 正格キーワード
217218
| string | 文字列
218219
| string interpolation | 文字列インターポーレーション
219220
| string slice | 文字列スライス

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
- [Rust 2018](rust-2018/index.md)
3535
- [Path and module system changes](rust-2018/path-changes.md)
3636
- [トレイト関数の匿名パラメータの非推奨化](rust-2018/trait-fn-parameters.md)
37-
- [New keywords](rust-2018/new-keywords.md)
37+
- [新しいキーワード](rust-2018/new-keywords.md)
3838
- [Method dispatch for raw pointers to inference variables](rust-2018/tyvar-behind-raw-pointer.md)
3939
- [Cargo changes](rust-2018/cargo.md)
4040

src/rust-2018/new-keywords.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,167 @@
1+
<!--
12
# New keywords
3+
-->
24

5+
# 新しいキーワード
6+
7+
<!--
38
![Minimum Rust version: 1.27](https://img.shields.io/badge/Minimum%20Rust%20Version-1.27-brightgreen.svg)
9+
-->
10+
11+
![導入 Rust バージョン: 1.27](https://img.shields.io/badge/%E5%B0%8E%E5%85%A5%20Rust%20%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3-1.27-brightgreen.svg)
412

13+
<!--
514
## Summary
15+
-->
616

17+
## 概要
18+
19+
<!--
720
- `dyn` is a [strict keyword][strict], in 2015 it is a [weak keyword].
821
- `async` and `await` are [strict keywords][strict].
922
- `try` is a [reserved keyword].
23+
-->
24+
25+
- `dyn`[正格キーワード][strict]です。2015 では[弱いキーワード]です。
26+
- `asnyc``await`[正格キーワード][strict]です。
27+
- `try`[予約キーワード]です。
1028

29+
<!--
1130
[strict]: https://doc.rust-lang.org/reference/keywords.html#strict-keywords
1231
[weak keyword]: https://doc.rust-lang.org/reference/keywords.html#weak-keywords
1332
[reserved keyword]: https://doc.rust-lang.org/reference/keywords.html#reserved-keywords
33+
-->
34+
35+
[strict]: https://doc.rust-lang.org/reference/keywords.html#strict-keywords
36+
[弱いキーワード]: https://doc.rust-lang.org/reference/keywords.html#weak-keywords
37+
[予約キーワード]: https://doc.rust-lang.org/reference/keywords.html#reserved-keywords
1438

39+
<!--
1540
## Motivation
41+
-->
42+
43+
## 動機
1644

45+
<!--
1746
### `dyn Trait` for trait objects
47+
-->
1848

49+
### トレイトオブジェクトを表す `dyn Trait`
50+
51+
<!--
1952
The `dyn Trait` feature is the new syntax for using trait objects. In short:
53+
-->
54+
55+
`dyn Trait` 機能は、トレイトオブジェクトを使うための新しい構文です。簡単に言うと:
2056

57+
<!--
2158
* `Box<Trait>` becomes `Box<dyn Trait>`
2259
* `&Trait` and `&mut Trait` become `&dyn Trait` and `&mut dyn Trait`
60+
-->
2361

62+
* `Box<Trait>``Box<dyn Trait>` になり、
63+
* `&Trait``&mut Trait``&dyn Trait``&mut dyn Trait` になる
64+
65+
<!--
2466
And so on. In code:
67+
-->
68+
69+
といった具合です。プログラム内では:
2570

2671
```rust
2772
trait Trait {}
2873

2974
impl Trait for i32 {}
3075

3176
// old
77+
// いままで
3278
fn function1() -> Box<Trait> {
3379
# unimplemented!()
3480
}
3581

3682
// new
83+
// これから
3784
fn function2() -> Box<dyn Trait> {
3885
# unimplemented!()
3986
}
4087
```
4188

89+
<!--
4290
That's it!
91+
-->
92+
93+
これだけです!
4394

95+
<!--
4496
#### Why?
97+
-->
4598

99+
#### なぜ?
100+
101+
<!--
46102
Using just the trait name for trait objects turned out to be a bad decision.
47103
The current syntax is often ambiguous and confusing, even to veterans,
48104
and favors a feature that is not more frequently used than its alternatives,
49105
is sometimes slower, and often cannot be used at all when its alternatives can.
106+
-->
107+
108+
トレイトオブジェクトにトレイト名をそのまま使うのは悪手だったと、後になって分かりました。
109+
今までの構文は、経験者にとってさえ往々にして曖昧にして難解で、代替機能を使うべきで本来お呼びでないような場面[^1]で頻繁に使われ、時には遅く、代替機能にはできることができないのです。
50110

111+
<!--
51112
Furthermore, with `impl Trait` arriving, "`impl Trait` vs `dyn Trait`" is much
52113
more symmetric, and therefore a bit nicer, than "`impl Trait` vs `Trait`".
53114
`impl Trait` is explained [here][impl-trait].
115+
-->
54116

117+
その上、`impl Trait` が入ったことで、「`impl Trait``dyn Trait` か」の関係はより対称的になり、「`impl Trait``Trait` か」よりちょっといい感じです。
118+
`impl Trait` の説明は[こちら][impl-trait]です。
119+
120+
<!--
55121
In the new edition, you should therefore prefer `dyn Trait` to just `Trait`
56122
where you need a trait object.
123+
-->
124+
125+
したがって、新しいエディションでは、トレイトオブジェクトが必要なときは、ただの `Trait` でなく `dyn Trait` を使うべきです。
57126

58127
<!--
59128
[impl-trait]: ../../rust-by-example/trait/impl_trait.html
60129
-->
61-
[impl-trait]: https://doc.rust-jp.rs/rust-by-example-ja/rust-by-example/trait/impl_trait.html
130+
[impl-trait]: https://doc.rust-jp.rs/rust-by-example-ja/trait/impl_trait.html
131+
62132

133+
> [^1] *訳注*:
134+
原文ではこの文は、本ページで説明する新構文を提案する [RFC](https://rust-lang.github.io/rfcs/2113-dyn-trait-syntax.html) から抜粋された文章になっています。
135+
特に脚注で示した箇所は、原文では "favors a feature that is not more frequently used than its alternatives" とあり、その文意は[同 RFC に解説されています](https://rust-lang.github.io/rfcs/2113-dyn-trait-syntax.html#favors-a-feature-that-is-not-more-frequently-used-than-its-alternatives)。以下では、それを要約します。
136+
特定のトレイトを実装した異なる型を共通して扱いたいとき、大抵はトレイトオブジェクトを使う必要はありません。
137+
単一のコンテナに複数の型の構造体を入れたい場合、`enum` を使えばよいです。
138+
関数の返り値が特定のトレイトを実装していると示すには、`impl Trait` 構文を使えばよいです。
139+
特定のトレイトを実装する任意の型を関数の引数や構造体のフィールドにした場合、ジェネリクスを使えばよいです。
140+
大抵の場合は、このようにセマンティクス面からもパフォーマンス面からもより適切な代替案があり、トレイトオブジェクトの出る幕はありません。
141+
トレイトオブジェクトが真に必要なのは、これより複雑なことをしたい場合だけです。
142+
しかし、Rust 2015 では、`&Trait` のように書くだけで、「気軽に」トレイトオブジェクトが作れてしまうという罠がありました。
143+
そこで、Rust 2018 では、どうしてもトレイトオブジェクトを作りたい場合は `&dyn Trait` 構文を使用することが必要になりました。
144+
145+
<!--
63146
### `async` and `await`
147+
-->
148+
149+
### `async``await`
64150

151+
<!--
65152
These keywords are reserved to implement the async-await feature of Rust, which was ultimately [released to stable in 1.39.0](https://blog.rust-lang.org/2019/11/07/Async-await-stable.html).
153+
-->
66154

155+
これらのキーワードは Rust に非同期の機能を実装するために予約されました。非同期の機能は[最終的に 1.39.0 でリリースされました](https://blog.rust-lang.org/2019/11/07/Async-await-stable.html)
156+
157+
<!--
67158
### `try` keyword
159+
-->
160+
161+
### キーワード `try`
68162

163+
<!--
69164
The `try` keyword is reserved for use in `try` blocks, which have not (as of this writing) been stabilized ([tracking issue](https://github.com/rust-lang/rust/issues/31436))
165+
-->
166+
167+
キーワード `try``try` ブロックで使うために予約されましたが、(これを書いている時点で)まだ安定化されていません([追跡イシュー](https://github.com/rust-lang/rust/issues/31436)

0 commit comments

Comments
 (0)