1
- % Box Syntax and Patterns
1
+ % Box構文とパターン
2
+ <!-- % Box Syntax and Patterns -->
2
3
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 ` の作成と分解の両方に使えます。使い方は以下の通りです。
7
9
8
10
``` rust
9
11
#![feature(box_syntax, box_patterns)]
@@ -25,14 +27,17 @@ fn main() {
25
27
}
26
28
```
27
29
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 ` (分解とパターンマッチ)を明示しなければ使えません。
31
34
32
- # Returning Pointers
35
+ <!-- # Returning Pointers -->
36
+ # ポインタ返し
33
37
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
+ ポインタを持つ多くのプログラミング言語では、巨大なデータ構造のコピーを避けるため、関数からポインタを返してきました。例えば以下のように書くことができます。
36
41
37
42
``` rust
38
43
struct BigStruct {
@@ -57,10 +62,12 @@ fn main() {
57
62
}
58
63
```
59
64
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 ` の代わりにポインタのみのコピーで済む、というものです。
62
68
63
- This is an antipattern in Rust. Instead, write this:
69
+ <!-- This is an antipattern in Rust. Instead, write this: -->
70
+ これはRustではアンチパターンです。代わりに以下のように書きます。
64
71
65
72
``` rust
66
73
#![feature(box_syntax)]
@@ -87,14 +94,17 @@ fn main() {
87
94
}
88
95
```
89
96
90
- This gives you flexibility without sacrificing performance.
97
+ <!-- This gives you flexibility without sacrificing performance. -->
98
+ このように書くことでパフォーマンスを犠牲にすることなく、柔軟性を確保することができます。
91
99
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 ` )の中に書き込みます。
97
106
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
+ 重要なことなので繰り返しますが、ポインタを戻り値の最適化のために使うべきではありません。呼び出し側が戻り値をどのように使うかを選択できるようにしましょう。
0 commit comments