Skip to content

Commit 5ce1b28

Browse files
authored
Merge pull request #155 from kdnakt/translate-dyn
Translate untranslated lines in dyn.md
2 parents 189bea6 + 9b0fbea commit 5ce1b28

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@
288288
-->
289289
- [トレイト](trait.md)
290290
- [導出(Derive)](trait/derive.md)
291-
- [Returning Traits with `dyn`](trait/dyn.md)
291+
- [`dyn`を利用してトレイトを返す](trait/dyn.md)
292292
- [演算子のオーバーロード](trait/ops.md)
293293
- [メモリ解放](trait/drop.md)
294294
- [イテレータ](trait/iter.md)

src/trait/dyn.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,59 @@
1+
<!--
12
# Returning Traits with `dyn`
3+
-->
4+
# `dyn`を利用してトレイトを返す
25

6+
<!--
37
The Rust compiler needs to know how much space every function's return type requires. This means all your functions have to return a concrete type. Unlike other languages, if you have a trait like `Animal`, you can't write a function that returns `Animal`, because its different implementations will need different amounts of memory.
8+
-->
9+
Rustのコンパイラはあらゆる関数のリターン型に必要なスペースを知っておく必要があります。
10+
つまり、すべての関数は具体的な型を返す必要があるのです。
11+
他の言語と違って、`Animal`のようなトレイトがある場合に、`Animal`を返す関数を書くことはできません。
12+
なぜなら、そのトレイトの異なる実装はそれぞれ別の量のメモリを必要とするからです。
413

14+
<!--
515
However, there's an easy workaround. Instead of returning a trait object directly, our functions return a `Box` which _contains_ some `Animal`. A `box` is just a reference to some memory in the heap. Because a reference has a statically-known size, and the compiler can guarantee it points to a heap-allocated `Animal`, we can return a trait from our function!
16+
-->
17+
しかし、簡単な回避策があります。
18+
直接トレイトオブジェクトを返す代わりに、`Animal`_含む_ `Box`を返すのです。
19+
`Box`はヒープ中のメモリへの単なる参照です。
20+
参照のサイズは静的に知ることができ、コンパイラは参照がヒープに割り当てられた`Animal`を指していると保証できるので、私たちは関数からトレイトを返すことができます。
621

22+
<!--
723
Rust tries to be as explicit as possible whenever it allocates memory on the heap. So if your function returns a pointer-to-trait-on-heap in this way, you need to write the return type with the `dyn` keyword, e.g. `Box<dyn Animal>`.
24+
-->
25+
ヒープにメモリを割り当てる際、Rustは可能な限り明示的であろうとします。
26+
なので、もしあなたの関数がヒープ上のトレイトへのポインタを返す場合、例えば`Box<dyn Animal>`のように、リターン型に`dyn`キーワードをつける必要があります。
827

928
```rust,editable
1029
struct Sheep {}
1130
struct Cow {}
1231
1332
trait Animal {
1433
// Instance method signature
34+
// インスタンスのメソッドシグネチャ
1535
fn noise(&self) -> &'static str;
1636
}
1737
1838
// Implement the `Animal` trait for `Sheep`.
39+
// `Sheep`に`Animal`トレイトを実装する。
1940
impl Animal for Sheep {
2041
fn noise(&self) -> &'static str {
2142
"baaaaah!"
2243
}
2344
}
2445
2546
// Implement the `Animal` trait for `Cow`.
47+
// `Cow`に`Animal`トレイトを実装する。
2648
impl Animal for Cow {
2749
fn noise(&self) -> &'static str {
2850
"moooooo!"
2951
}
3052
}
3153
3254
// Returns some struct that implements Animal, but we don't know which one at compile time.
55+
// Animalを実装した何らかの構造体を返す。
56+
// ただし、コンパイル時にはどの実装か分からない。
3357
fn random_animal(random_number: f64) -> Box<dyn Animal> {
3458
if random_number < 0.5 {
3559
Box::new(Sheep {})

0 commit comments

Comments
 (0)