|
4 | 4 | <meta charset="utf-8">
|
5 | 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 | 6 | <meta name="generator" content="rustdoc">
|
7 |
| - <title>Box Syntax and Patterns</title> |
| 7 | + <title>Box構文とパターン</title> |
8 | 8 |
|
9 | 9 | <link rel="stylesheet" type="text/css" href="rustbook.css">
|
10 | 10 |
|
|
185 | 185 | <div id='page'>
|
186 | 186 |
|
187 | 187 |
|
188 |
| - <h1 class="title">Box Syntax and Patterns</h1> |
189 |
| - <p>Currently the only stable way to create a <code>Box</code> is via the <code>Box::new</code> method. |
190 |
| -Also it is not possible in stable Rust to destructure a <code>Box</code> in a match |
191 |
| -pattern. The unstable <code>box</code> keyword can be used to both create and destructure |
192 |
| -a <code>Box</code>. An example usage would be:</p> |
| 188 | + <h1 class="title">Box構文とパターン</h1> |
| 189 | + <!-- % Box Syntax and Patterns --> |
| 190 | + |
| 191 | +<!-- Currently the only stable way to create a `Box` is via the `Box::new` method. --> |
| 192 | + |
| 193 | +<!-- Also it is not possible in stable Rust to destructure a `Box` in a match --> |
| 194 | + |
| 195 | +<!-- pattern. The unstable `box` keyword can be used to both create and destructure --> |
| 196 | + |
| 197 | +<!-- a `Box`. An example usage would be: --> |
| 198 | + |
| 199 | +<p>今のところ、安定版において <code>Box</code> を作成する唯一の方法は <code>Box::new</code> メソッドです。安定版のRustではパターンマッチで <code>Box</code> を分解することもできません。不安定版の <code>box</code> キーワードは <code>Box</code> の作成と分解の両方に使えます。使い方は以下の通りです。</p> |
193 | 200 | <span class='rusttest'>#![feature(box_syntax, box_patterns)]
|
194 | 201 |
|
195 | 202 | fn main() {
|
@@ -226,13 +233,22 @@ <h1 class="title">Box Syntax and Patterns</h1>
|
226 | 233 | }
|
227 | 234 | }</pre>
|
228 | 235 |
|
229 |
| -<p>Note that these features are currently hidden behind the <code>box_syntax</code> (box |
230 |
| -creation) and <code>box_patterns</code> (destructuring and pattern matching) gates |
231 |
| -because the syntax may still change in the future.</p> |
| 236 | +<!-- Note that these features are currently hidden behind the `box_syntax` (box --> |
| 237 | + |
| 238 | +<!-- creation) and `box_patterns` (destructuring and pattern matching) gates --> |
232 | 239 |
|
233 |
| -<h1 id='returning-pointers' class='section-header'><a href='#returning-pointers'>Returning Pointers</a></h1> |
234 |
| -<p>In many languages with pointers, you'd return a pointer from a function |
235 |
| -so as to avoid copying a large data structure. For example:</p> |
| 240 | +<!-- because the syntax may still change in the future. --> |
| 241 | + |
| 242 | +<p>注記: 将来的にこの構文は変わる可能性があるため、現時点でこれらのフィーチャは <code>box_syntax</code> (boxの作成)、 <code>box_patterns</code> (分解とパターンマッチ)を明示しなければ使えません。</p> |
| 243 | + |
| 244 | +<!-- # Returning Pointers --> |
| 245 | + |
| 246 | +<h1 id='ポインタ返し' class='section-header'><a href='#ポインタ返し'>ポインタ返し</a></h1> |
| 247 | +<!-- In many languages with pointers, you'd return a pointer from a function --> |
| 248 | + |
| 249 | +<!-- so as to avoid copying a large data structure. For example: --> |
| 250 | + |
| 251 | +<p>ポインタを持つ多くのプログラミング言語では、巨大なデータ構造のコピーを避けるため、関数からポインタを返してきました。例えば以下のように書くことができます。</p> |
236 | 252 | <span class='rusttest'>struct BigStruct {
|
237 | 253 | one: i32,
|
238 | 254 | two: i32,
|
@@ -275,10 +291,15 @@ <h1 id='returning-pointers' class='section-header'><a href='#returning-pointers'
|
275 | 291 | <span class='kw'>let</span> <span class='ident'>y</span> <span class='op'>=</span> <span class='ident'>foo</span>(<span class='ident'>x</span>);
|
276 | 292 | }</pre>
|
277 | 293 |
|
278 |
| -<p>The idea is that by passing around a box, you're only copying a pointer, rather |
279 |
| -than the hundred <code>i32</code>s that make up the <code>BigStruct</code>.</p> |
| 294 | +<!-- The idea is that by passing around a box, you're only copying a pointer, rather --> |
| 295 | + |
| 296 | +<!-- than the hundred `i32`s that make up the `BigStruct`. --> |
280 | 297 |
|
281 |
| -<p>This is an antipattern in Rust. Instead, write this:</p> |
| 298 | +<p>考え方としては、boxで渡すことで <code>BigStruct</code> を構成する100個の <code>i32</code> の代わりにポインタのみのコピーで済む、というものです。</p> |
| 299 | + |
| 300 | +<!-- This is an antipattern in Rust. Instead, write this: --> |
| 301 | + |
| 302 | +<p>これはRustではアンチパターンです。代わりに以下のように書きます。</p> |
282 | 303 | <span class='rusttest'>#![feature(box_syntax)]
|
283 | 304 |
|
284 | 305 | struct BigStruct {
|
@@ -325,17 +346,29 @@ <h1 id='returning-pointers' class='section-header'><a href='#returning-pointers'
|
325 | 346 | <span class='kw'>let</span> <span class='ident'>y</span>: <span class='ident'>Box</span><span class='op'><</span><span class='ident'>BigStruct</span><span class='op'>></span> <span class='op'>=</span> <span class='kw'>box</span> <span class='ident'>foo</span>(<span class='ident'>x</span>);
|
326 | 347 | }</pre>
|
327 | 348 |
|
328 |
| -<p>This gives you flexibility without sacrificing performance.</p> |
| 349 | +<!-- This gives you flexibility without sacrificing performance. --> |
| 350 | + |
| 351 | +<p>このように書くことでパフォーマンスを犠牲にすることなく、柔軟性を確保することができます。</p> |
| 352 | + |
| 353 | +<!-- You may think that this gives us terrible performance: return a value and then --> |
| 354 | + |
| 355 | +<!-- immediately box it up ?! Isn't this pattern the worst of both worlds? Rust is --> |
| 356 | + |
| 357 | +<!-- smarter than that. There is no copy in this code. `main` allocates enough room --> |
| 358 | + |
| 359 | +<!-- for the `box`, passes a pointer to that memory into `foo` as `x`, and then --> |
| 360 | + |
| 361 | +<!-- `foo` writes the value straight into the `Box<T>`. --> |
| 362 | + |
| 363 | +<p>このコードはひどいパフォーマンス低下をもたらすと感じるかもしれません。値を返して即座にboxに入れるなんて?! このパターンは悪いところどりになっているのでは? Rustはもう少し賢いため、このコードでコピーは発生しません。 <code>main</code> 内では <code>box</code> のために十分なメモリ領域を確保し、そのメモリへのポインタを <code>foo</code> へ <code>x</code> として渡します。 <code>foo</code> はその値を直接 <code>Box<T></code> (訳注: すなわち <code>y</code> )の中に書き込みます。</p> |
| 364 | + |
| 365 | +<!-- This is important enough that it bears repeating: pointers are not for --> |
| 366 | + |
| 367 | +<!-- optimizing returning values from your code. Allow the caller to choose how they --> |
329 | 368 |
|
330 |
| -<p>You may think that this gives us terrible performance: return a value and then |
331 |
| -immediately box it up ?! Isn't this pattern the worst of both worlds? Rust is |
332 |
| -smarter than that. There is no copy in this code. <code>main</code> allocates enough room |
333 |
| -for the <code>box</code>, passes a pointer to that memory into <code>foo</code> as <code>x</code>, and then |
334 |
| -<code>foo</code> writes the value straight into the <code>Box<T></code>.</p> |
| 369 | +<!-- want to use your output. --> |
335 | 370 |
|
336 |
| -<p>This is important enough that it bears repeating: pointers are not for |
337 |
| -optimizing returning values from your code. Allow the caller to choose how they |
338 |
| -want to use your output.</p> |
| 371 | +<p>重要なことなので繰り返しますが、ポインタを戻り値の最適化のために使うべきではありません。呼び出し側が戻り値をどのように使うかを選択できるようにしましょう。</p> |
339 | 372 |
|
340 | 373 | <script type="text/javascript">
|
341 | 374 | window.playgroundUrl = "https://play.rust-lang.org";
|
|
0 commit comments