Skip to content

Commit 5ea2f9c

Browse files
committed
Merge pull request #37 from dalance/box-syntax-and-pattarns
6.8. Box Syntax and Patterns
2 parents 5b875b3 + 76f7952 commit 5ea2f9c

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

1.6/ja/book/box-syntax-and-patterns.md

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
% Box Syntax and Patterns
1+
% Box構文とパターン
2+
<!-- % Box Syntax and Patterns -->
23

3-
Currently the only stable way to create a `Box` is via the `Box::new` method.
4-
Also it is not possible in stable Rust to destructure a `Box` in a match
5-
pattern. The unstable `box` keyword can be used to both create and destructure
6-
a `Box`. An example usage would be:
4+
<!-- Currently the only stable way to create a `Box` is via the `Box::new` method. -->
5+
<!-- Also it is not possible in stable Rust to destructure a `Box` in a match -->
6+
<!-- pattern. The unstable `box` keyword can be used to both create and destructure -->
7+
<!-- a `Box`. An example usage would be: -->
8+
今のところ、安定版において `Box` を作成する唯一の方法は `Box::new` メソッドです。安定版のRustではパターンマッチで `Box` を分解することもできません。不安定版の `box` キーワードは `Box` の作成と分解の両方に使えます。使い方は以下の通りです。
79

810
```rust
911
#![feature(box_syntax, box_patterns)]
@@ -25,14 +27,17 @@ fn main() {
2527
}
2628
```
2729

28-
Note that these features are currently hidden behind the `box_syntax` (box
29-
creation) and `box_patterns` (destructuring and pattern matching) gates
30-
because the syntax may still change in the future.
30+
<!-- Note that these features are currently hidden behind the `box_syntax` (box -->
31+
<!-- creation) and `box_patterns` (destructuring and pattern matching) gates -->
32+
<!-- because the syntax may still change in the future. -->
33+
注記: 将来的にこの構文は変わる可能性があるため、現時点でこれらのフィーチャは `box_syntax` (boxの作成)、 `box_patterns` (分解とパターンマッチ)を明示しなければ使えません。
3134

32-
# Returning Pointers
35+
<!-- # Returning Pointers -->
36+
# ポインタ返し
3337

34-
In many languages with pointers, you'd return a pointer from a function
35-
so as to avoid copying a large data structure. For example:
38+
<!-- In many languages with pointers, you'd return a pointer from a function -->
39+
<!-- so as to avoid copying a large data structure. For example: -->
40+
ポインタを持つ多くのプログラミング言語では、巨大なデータ構造のコピーを避けるため、関数からポインタを返してきました。例えば以下のように書くことができます。
3641

3742
```rust
3843
struct BigStruct {
@@ -57,10 +62,12 @@ fn main() {
5762
}
5863
```
5964

60-
The idea is that by passing around a box, you're only copying a pointer, rather
61-
than the hundred `i32`s that make up the `BigStruct`.
65+
<!-- The idea is that by passing around a box, you're only copying a pointer, rather -->
66+
<!-- than the hundred `i32`s that make up the `BigStruct`. -->
67+
考え方としては、boxで渡すことで `BigStruct` を構成する100個の `i32` の代わりにポインタのみのコピーで済む、というものです。
6268

63-
This is an antipattern in Rust. Instead, write this:
69+
<!-- This is an antipattern in Rust. Instead, write this: -->
70+
これはRustではアンチパターンです。代わりに以下のように書きます。
6471

6572
```rust
6673
#![feature(box_syntax)]
@@ -87,14 +94,17 @@ fn main() {
8794
}
8895
```
8996

90-
This gives you flexibility without sacrificing performance.
97+
<!-- This gives you flexibility without sacrificing performance. -->
98+
このように書くことでパフォーマンスを犠牲にすることなく、柔軟性を確保することができます。
9199

92-
You may think that this gives us terrible performance: return a value and then
93-
immediately box it up ?! Isn't this pattern the worst of both worlds? Rust is
94-
smarter than that. There is no copy in this code. `main` allocates enough room
95-
for the `box`, passes a pointer to that memory into `foo` as `x`, and then
96-
`foo` writes the value straight into the `Box<T>`.
100+
<!-- You may think that this gives us terrible performance: return a value and then -->
101+
<!-- immediately box it up ?! Isn't this pattern the worst of both worlds? Rust is -->
102+
<!-- smarter than that. There is no copy in this code. `main` allocates enough room -->
103+
<!-- for the `box`, passes a pointer to that memory into `foo` as `x`, and then -->
104+
<!-- `foo` writes the value straight into the `Box<T>`. -->
105+
このコードはひどいパフォーマンス低下をもたらすと感じるかもしれません。値を返して即座にboxに入れるなんて?! このパターンは悪いところどりになっているのでは? Rustはもう少し賢いため、このコードでコピーは発生しません。 `main` 内では `box` のために十分なメモリ領域を確保し、そのメモリへのポインタを `foo``x` として渡します。 `foo` はその値を直接 `Box<T>` (訳注: すなわち `y` )の中に書き込みます。
97106

98-
This is important enough that it bears repeating: pointers are not for
99-
optimizing returning values from your code. Allow the caller to choose how they
100-
want to use your output.
107+
<!-- This is important enough that it bears repeating: pointers are not for -->
108+
<!-- optimizing returning values from your code. Allow the caller to choose how they -->
109+
<!-- want to use your output. -->
110+
重要なことなので繰り返しますが、ポインタを戻り値の最適化のために使うべきではありません。呼び出し側が戻り値をどのように使うかを選択できるようにしましょう。

TranslationTable.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
|:-------------------------------|:-------------
1313
| alignment | アラインメント
1414
| allocator | アロケータ
15+
| antipattern | アンチパターン
1516
| application | アプリケーション
1617
| arity | アリティ
1718
| array | 配列
@@ -44,6 +45,7 @@
4445
| generics | ジェネリクス
4546
| identifier | 識別子
4647
| immutable | イミュータブル
48+
| keyword | キーワード
4749
| Intrinsics | Intrinsic
4850
| Lang Items | Lang Item
4951
| library | ライブラリ
@@ -52,13 +54,17 @@
5254
| lint | リント
5355
| match | マッチ
5456
| memory | メモリ
57+
| method | メソッド
5558
| move | ムーブ
5659
| mutable | ミュータブル
5760
| mutability | ミュータビリティ
5861
| owner | 所有者
5962
| ownership | 所有権
6063
| panic | パニック
6164
| parametric polymorphism | パラメトリック多相
65+
| pattern | パターン
66+
| performance | パフォーマンス
67+
| pointer | ポインタ
6268
| return | 返す
6369
| shadow | 覆い隠す
6470
| signature | シグネチャ

0 commit comments

Comments
 (0)