1 # Japanese translations for Rust package
2 # Copyright (C) 2013 The Rust Project Developers
3 # This file is distributed under the same license as the Rust package.
4 # Automatically generated, 2013.
8 "Project-Id-Version: Rust 0.8\n"
9 "POT-Creation-Date: 2014-02-03 08:13+0900\n"
10 "PO-Revision-Date: 2013-08-08 22:27+0900\n"
11 "Last-Translator: Automatically generated\n"
12 "Language-Team: none\n"
15 "Content-Type: text/plain; charset=UTF-8\n"
16 "Content-Transfer-Encoding: UTF-8\n"
17 "Plural-Forms: nplurals=1; plural=0;\n"
20 #: src/doc/guide-conditions.md:4 src/doc/guide-ffi.md:4
21 #: src/doc/guide-lifetimes.md:4 src/doc/guide-macros.md:4
22 #: src/doc/guide-tasks.md:4 src/doc/rust.md:4 src/doc/tutorial.md:4
23 msgid "# Introduction"
27 #: src/doc/guide-ffi.md:168 src/doc/tutorial.md:885
32 #: src/doc/guide-lifetimes.md:72 src/doc/tutorial.md:1409
33 msgid "Now we can call `compute_distance()` in various ways:"
35 "上記の `compute_distance()` 関数は、様々な方法で呼び出すことができます。"
38 #: src/doc/guide-pointers.md:326 src/doc/tutorial.md:1360
41 msgstr "# ポインタのデリファレンス"
44 #: src/doc/guide-testing.md:127 src/doc/tutorial.md:786
50 #: src/doc/rust.md:1391 src/doc/tutorial.md:2495
51 msgid "Likewise, supertrait methods may also be called on trait objects."
53 "同様に、スーパートレイトのメソッドは、トレイトオブジェクトについても呼び出す"
57 #: src/doc/tutorial.md:2
58 msgid "% The Rust Language Tutorial"
59 msgstr "% Rust 言語チュートリアル"
62 #: src/doc/tutorial.md:13
64 "Rust is a programming language with a focus on type safety, memory safety, "
65 "concurrency and performance. It is intended for writing large-scale, high-"
66 "performance software that is free from several classes of common errors. "
67 "Rust has a sophisticated memory model that encourages efficient data "
68 "structures and safe concurrency patterns, forbidding invalid memory accesses "
69 "that would otherwise cause segmentation faults. It is statically typed and "
70 "compiled ahead of time."
72 "Rust は、型安全性、メモリ安全性、並列性、パフォーマンスを重視したプログラミン"
73 "グ言語です。大規模でハイパフォーマンスなソフトウェア作成向きの、C++ 等の言語"
74 "でよくある誤りを犯しにくい言語仕様になっています。Rust の洗練されたメモリモデ"
75 "ルでは、効率的なデータ構造や安全な並行計算を実現することが可能であり、セグメ"
76 "ンテーション違反を引き起こす不正なメモリアクセスは起こりえません。Rust は、静"
77 "的型付けされた、コンパイル型の言語です。"
80 #: src/doc/tutorial.md:17
82 "As a multi-paradigm language, Rust supports writing code in procedural, "
83 "functional and object-oriented styles. Some of its pleasant high-level "
86 "Rust はマルチパラダイム言語であり、手続き型、関数型、オブジェクト指向型のプロ"
87 "グラミングをサポートします。Rust には、次のような素敵で高レベルな特徴がありま"
91 #: src/doc/tutorial.md:30
93 "**Type inference.** Type annotations on local variable declarations are "
96 "**型推論:** ローカル変数の宣言では型注釈 (type annotation) を省略できます。"
99 #: src/doc/tutorial.md:30
101 "**Safe task-based concurrency.** Rust's lightweight tasks do not share "
102 "memory, instead communicating through messages."
104 "**タスクベースの安全な並行計算:** Rust の軽量タスクはタスク間でメモリを共有す"
105 "るのではなく、メッセージを介して通信します。"
107 #. type: Bullet: '* '
108 #: src/doc/tutorial.md:30
110 "**Higher-order functions.** Efficient and flexible closures provide "
111 "iteration and other control structures"
113 "**高階関数:** 効率的で柔軟なクロージャにより、イテレーションやその他の制御構"
116 #. type: Bullet: '* '
117 #: src/doc/tutorial.md:30
119 "**Pattern matching and algebraic data types.** Pattern matching on Rust's "
120 "enumeration types (a more powerful version of C's enums, similar to "
121 "algebraic data types in functional languages) is a compact and expressive "
122 "way to encode program logic."
124 "**パターンマッチと代数的データ型:** Rust の列挙型 (C の列挙型の強化バージョ"
125 "ン。関数型言語における代数的データ型のようなもの) のパターンマッチにより、プ"
126 "ログラムの論理を、コンパクトで表現力豊かに記述することができます。"
128 #. type: Bullet: '* '
129 #: src/doc/tutorial.md:30
131 "**Polymorphism.** Rust has type-parametric functions and types, type classes "
132 "and OO-style interfaces."
134 "**ポリモーフィズム:** Rust には、型パラメータをもつ関数や型、型クラスとオブ"
135 "ジェクト指向風のインターフェースがあります。"
138 #: src/doc/tutorial.md:32
143 #: src/doc/tutorial.md:38
145 "This is an introductory tutorial for the Rust programming language. It "
146 "covers the fundamentals of the language, including the syntax, the type "
147 "system and memory model, generics, and modules. [Additional tutorials](#what-"
148 "next) cover specific language features in greater depth."
150 "この文章は、Rust プログラミング言語のチュートリアルです。構文、型システムとメ"
151 "モリモデル、ジェネリクス、モジュールなどの言語の基礎となる部分をカバーしてい"
152 "ます。 いくつかの言語機能の詳細については、 [その他のチュートリアル](#次のス"
156 #: src/doc/tutorial.md:42
158 "This tutorial assumes that the reader is already familiar with one or more "
159 "languages in the C family. Understanding of pointers and general memory "
160 "management techniques will help."
162 "本チュートリアルは、読者がすでに 1 つ以上の C 系統言語 に精通していることを前"
163 "提としています。本書を理解する上で、ポインタやメモリ管理のテクニックに関する"
167 #: src/doc/tutorial.md:44
168 msgid "## Conventions"
169 msgstr "## 本書の表記について"
172 #: src/doc/tutorial.md:47
174 "Throughout the tutorial, language keywords and identifiers defined in "
175 "example code are displayed in `code font`."
177 "本チュートリアルでは、言語のキーワードや、サンプルコード中で定義される識別子"
178 "を `code font` のように示します。"
181 #: src/doc/tutorial.md:53
183 "Code snippets are indented, and also shown in a monospaced font. Not all "
184 "snippets constitute whole programs. For brevity, we'll often show fragments "
185 "of programs that don't compile on their own. To try them out, you might have "
186 "to wrap them in `fn main() { ... }`, and make sure they don't contain "
187 "references to names that aren't actually defined."
189 "コードスニペットは、インデントされ、固定幅フォントで表示されます。いくつかの"
190 "コードスニペットは、プログラムの一部を抜き出したものになっています。説明を簡"
191 "潔にするため、それだけではコンパイル不可能なプログラムの断片を掲載することが"
192 "あります。プログラムの動作を試す際には、全体を `fn main() { ... }` で囲んでく"
193 "ださい。また、プログラム中で未定義の名前を参照している箇所がないか確認してく"
197 #: src/doc/tutorial.md:57
199 "> *Warning:* Rust is a language under ongoing development. Notes > about "
200 "potential changes to the language, implementation > deficiencies, and other "
201 "caveats appear offset in blockquotes."
203 "> *警告:* Rust は開発途上の言語です。将来予定されている言語への変更や、実"
204 "装上の不備、その他の注意事項など、 blockquote の段落 (この段落もそうです) に"
208 #: src/doc/tutorial.md:59
209 msgid "# Getting started"
213 #: src/doc/tutorial.md:67
216 #| "The Rust compiler currently must be built from a [tarball], unless you "
217 #| "are on Windows, in which case using the [installer][win-exe] is "
220 "The Rust compiler currently must be built from a [tarball] or [git], unless "
221 "you are on Windows, in which case using the [installer][win-exe] is "
222 "recommended. There is a list of community-maintained nightly builds and "
223 "packages [on the wiki][wiki-packages]."
225 "Windows 以外の環境では、今のところ、Rust のコンパイラは [ソースコード]"
226 "[tarball] からビルドする必要があります。Windows をお使いの場合は、 [インス"
227 "トーラー][win-exe] の使用をおすすめします。"
230 #: src/doc/tutorial.md:72
233 #| "Since the Rust compiler is written in Rust, it must be built by a "
234 #| "precompiled \"snapshot\" version of itself (made in an earlier state of "
235 #| "development). As such, source builds require a connection to the "
236 #| "Internet, to fetch snapshots, and an OS that can execute the available "
237 #| "snapshot binaries."
239 "Since the Rust compiler is written in Rust, it must be built by a "
240 "precompiled \"snapshot\" version of itself (made in an earlier state of "
241 "development). The source build automatically fetches these snapshots from "
242 "the Internet on our supported platforms."
244 "Rust のコンパイラは Rust で書かれているため、最新版の少し前のソースからコンパ"
245 "イルされた「スナップショット」版コンパイラでビルドする必要があります。スナッ"
246 "プショットを利用してビルドするためには、スナップショットをダウンロードするた"
247 "めのインターネット接続と、スナップショットバイナリを実行できる OS が必要で"
251 #: src/doc/tutorial.md:74
252 msgid "Snapshot binaries are currently built and tested on several platforms:"
254 "スナップショットバイナリは、現時点では以下のプラットフォーム向けのものがあり"
257 #. type: Bullet: '* '
258 #: src/doc/tutorial.md:78
260 #| msgid "Windows (7, Server 2008 R2), x86 only"
261 msgid "Windows (7, 8, Server 2008 R2), x86 only"
262 msgstr "Windows (7, Server 2008 R2), x86 のみ"
264 #. type: Bullet: '* '
265 #: src/doc/tutorial.md:78
267 #| msgid "Linux (various distributions), x86 and x86-64"
268 msgid "Linux (2.6.18 or later, various distributions), x86 and x86-64"
269 msgstr "Linux (各種ディストリビューション), x86 または x86-64"
271 #. type: Bullet: '* '
272 #: src/doc/tutorial.md:78
274 #| msgid "OSX 10.6 (\"Snow Leopard\") or greater, x86 and x86-64"
275 msgid "OSX 10.7 (Lion) or greater, x86 and x86-64"
276 msgstr "OSX 10.6 (\"Snow Leopard\") 以降, x86 または x86-64"
279 #: src/doc/tutorial.md:81
281 "You may find that other platforms work, but these are our \"tier 1\" "
282 "supported build environments that are most likely to work."
284 "スナップショットは他のプラットフォームでも動作するかもしれませんが、\"tier "
285 "1\" プラットフォームとしてサポートされているのは上記のみです。"
288 #: src/doc/tutorial.md:88
290 "> *Note:* Windows users should read the detailed > \"[getting started]"
291 "[wiki-start]\" notes on the wiki. Even when using > the binary installer, "
292 "the Windows build requires a MinGW installation, > the precise details of "
293 "which are not discussed here. Finally, `rustc` may > need to be [referred to "
294 "as `rustc.exe`][bug-3319]. It's a bummer, we > know."
296 "> *注意:* Windows ユーザーは wiki の [getting started][wiki-start] の記事"
297 "を読んでください。 本書では詳細を説明しませんが、インストーラを利用する場合で"
298 "も、MinGW のインストールなど、追加の手順が必要です。また、コンパイラは "
299 "`rustc` ではなく、 [`rustc.exe` として呼び出す必要がある][bug-3319] かもしれ"
300 "ません。このような制限があることはは、本当に残念です。"
303 #: src/doc/tutorial.md:92
306 #| "[bug-3319]: https://github.com/mozilla/rust/issues/3319 [wiki-start]: "
307 #| "https://github.com/mozilla/rust/wiki/Note-getting-started-developing-Rust"
309 "[bug-3319]: https://github.com/mozilla/rust/issues/3319 [wiki-start]: "
310 "https://github.com/mozilla/rust/wiki/Note-getting-started-developing-Rust "
311 "[git]: https://github.com/mozilla/rust.git"
313 "[bug-3319]: https://github.com/mozilla/rust/issues/3319\n"
314 "[wiki-start]: https://github.com/mozilla/rust/wiki/Note-getting-started-"
318 #: src/doc/tutorial.md:95
320 "To build from source you will also need the following prerequisite packages:"
321 msgstr "ソースからビルドするためには、以下のパッケージが必要です。"
323 #. type: Bullet: '* '
324 #: src/doc/tutorial.md:101
325 msgid "g++ 4.4 or clang++ 3.x"
326 msgstr "g++ 4.4 または clang++ 3.x"
328 #. type: Bullet: '* '
329 #: src/doc/tutorial.md:101
330 msgid "python 2.6 or later (but not 3.x)"
331 msgstr "python 2.6 以降 (ただし、 3.x は除く)"
333 #. type: Bullet: '* '
334 #: src/doc/tutorial.md:101
335 msgid "perl 5.0 or later"
338 #. type: Bullet: '* '
339 #: src/doc/tutorial.md:101
340 msgid "gnu make 3.81 or later"
341 msgstr "gnu make 3.81 以降"
343 #. type: Bullet: '* '
344 #: src/doc/tutorial.md:101
349 #: src/doc/tutorial.md:104
351 "If you've fulfilled those prerequisites, something along these lines should "
353 msgstr "上記条件を満たしていれば、以下のような手順でビルド可能なはずです。"
356 #: src/doc/tutorial.md:112
359 #| "~~~~ {.notrust} $ curl -O http://static.rust-lang.org/dist/rust-nightly.tar."
360 #| "gz $ tar -xzf rust-nightly.tar.gz $ cd rust-nightly $ ./configure $ make && make "
363 "~~~~ {.notrust} $ curl -O http://static.rust-lang.org/dist/rust-nightly.tar.gz $ "
364 "tar -xzf rust-nightly.tar.gz $ cd rust-nightly $ ./configure $ make && make install "
368 "$ curl -O http://static.rust-lang.org/dist/rust-nightly.tar.gz\n"
369 "$ tar -xzf rust-nightly.tar.gz $ cd rust-nightly $ ./configure\n"
370 "$ make && make install\n"
374 #: src/doc/tutorial.md:118
376 "You may need to use `sudo make install` if you do not normally have "
377 "permission to modify the destination directory. The install locations can be "
378 "adjusted by passing a `--prefix` argument to `configure`. Various other "
379 "options are also supported: pass `--help` for more information on them."
381 "インストール先のディレクトリを変更する権限がない場合、 `sudo make install` を"
382 "実行する必要があるかもしれません。インストール先は `configure` に `--prefix` "
383 "引数を渡すことで変更できます。`configure` はその他いろいろなオプションをサ"
384 "ポートしています。詳細については、 `--help` 引数を指定して実行してください。"
387 #: src/doc/tutorial.md:122
390 #| "When complete, `make install` will place several programs into `/usr/"
391 #| "local/bin`: `rustc`, the Rust compiler; `rustdoc`, the API-documentation "
392 #| "tool; and `rustpkg`, the Rust package manager."
394 "When complete, `make install` will place several programs into `/usr/local/"
395 "bin`: `rustc`, the Rust compiler, and `rustdoc`, the API-documentation tool."
397 "ビルド完了後、`make install` を実行すると、以下のプログラムが `/usr/local/"
398 "bin` にインストールされます。\n"
400 "* `rustc`: Rust のコンパイラ\n"
401 "* `rustdoc`: API ドキュメント作成ツール\n"
402 "* `rustpkg`: Rust のパッケージマネージャ\n"
405 #: src/doc/tutorial.md:125
408 #| "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz [win-exe]: "
409 #| "http://static.rust-lang.org/dist/rust-nightly-install.exe"
411 "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz [win-exe]: "
412 "http://static.rust-lang.org/dist/rust-nightly-install.exe"
414 "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz\n"
415 "[win-exe]: http://static.rust-lang.org/dist/rust-nightly-install.exe"
418 #: src/doc/tutorial.md:127
419 msgid "## Compiling your first program"
423 #: src/doc/tutorial.md:130
425 "Rust program files are, by convention, given the extension `.rs`. Say we "
426 "have a file `hello.rs` containing this program:"
428 "Rust プログラムのファイルの拡張子は、慣例的に `.rs` とされています。以下の内"
429 "容を持つファイル、`hello.rs` が存在するとします。"
432 #: src/doc/tutorial.md:136
434 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
438 " println!(\"hello?\");\n"
444 "extern crate world;\n"
445 "fn main() { println(~\"hello \" + world::explore()); }\n"
449 #: src/doc/tutorial.md:140
451 "If the Rust compiler was installed successfully, running `rustc hello.rs` "
452 "will produce an executable called `hello` (or `hello.exe` on Windows) which, "
453 "upon running, will likely do exactly what you expect."
455 "Rust のコンパイラが正しくインストールされている場合、 `rustc hello.rs` を実行"
456 "することで、実行ファイル `hello` (Windows の場合 `hello.exe`) を生成すること"
457 "が可能です。このファイルを実行すれば、予想通りの出力が得られるでしょう。"
460 #: src/doc/tutorial.md:145
463 #| "The Rust compiler tries to provide useful information when it encounters "
464 #| "an error. If you introduce an error into the program (for example, by "
465 #| "changing `println` to some nonexistent function), and then compile it, "
466 #| "you'll see an error message like this:"
468 "The Rust compiler tries to provide useful information when it encounters an "
469 "error. If you introduce an error into the program (for example, by changing "
470 "`println!` to some nonexistent macro), and then compile it, you'll see an "
471 "error message like this:"
473 "コンパイルエラーが発生した場合、Rust コンパイラはエラー解決のための情報を出力"
474 "します。プログラム中にエラーが含まれる場合 (上記プログラムの `println` を存在"
475 "しない別の名前に変更した場合など)、コンパイル時に以下のようなエラーメッセージ"
479 #: src/doc/tutorial.md:158
482 #| "In its simplest form, a Rust program is a `.rs` file with some types and "
483 #| "functions defined in it. If it has a `main` function, it can be compiled "
484 #| "to an executable. Rust does not allow code that's not a declaration to "
485 #| "appear at the top level of the file: all statements must live inside a "
486 #| "function. Rust programs can also be compiled as libraries, and included "
487 #| "in other programs."
489 "In its simplest form, a Rust program is a `.rs` file with some types and "
490 "functions defined in it. If it has a `main` function, it can be compiled to "
491 "an executable. Rust does not allow code that's not a declaration to appear "
492 "at the top level of the file: all statements must live inside a function. "
493 "Rust programs can also be compiled as libraries, and included in other "
494 "programs, even ones not written in Rust."
496 "最も単純な Rust プログラムは、いくつかの型や関数が定義された `.rs` ファイルの"
497 "みからなります。`.rs` ファイルをコンパイルして実行可能ファイルを作成する場"
498 "合、ファイル中に `main` 関数が含まれている必要があります。Rust プログラムで"
499 "ファイルのトップレベルに記述可能なのは宣言 (declaration) のみです。ステートメ"
500 "ント (statement) は、すべて関数内部に記述されます。Rust のプログラムは、他の"
501 "プログラムから利用するライブラリとしてコンパイルすることも可能です。"
504 #: src/doc/tutorial.md:160
505 msgid "## Editing Rust code"
506 msgstr "## Rust のコードを編集する"
509 #: src/doc/tutorial.md:170
511 "There are vim highlighting and indentation scripts in the Rust source "
512 "distribution under `src/etc/vim/`. There is an emacs mode under `src/etc/"
513 "emacs/` called `rust-mode`, but do read the instructions included in that "
514 "directory. In particular, if you are running emacs 24, then using emacs's "
515 "internal package manager to install `rust-mode` is the easiest way to keep "
516 "it up to date. There is also a package for Sublime Text 2, available both "
517 "[standalone][sublime] and through [Sublime Package Control][sublime-pkg], "
518 "and support for Kate under `src/etc/kate`."
520 "vim のハイライトとインデント用スクリプトは、Rust のソースツリーの `src/etc/"
521 "vim` 以下にあります。emacs の Rust 編集用モードの `rust-mode` は、 `src/etc/"
522 "emacs/` 以下にありますが、当該ディレクトリに配置されているインストール手順に"
523 "従って操作する必要があります。 emacs 24 以降をご利用の場合、 emacs 組み込みの"
524 "パッケージマネージャで `rust-mode` をインストールするのが最もお手軽です。"
525 "Sublime Text 2 用のパッケージは、[スタンドアロン版][sublime] と、[Sublime "
526 "Package Control][sublime-pkg] 版の2つがあります。Kate 向けパッケージは `src/"
530 #: src/doc/tutorial.md:177
532 "There is ctags support via `src/etc/ctags.rust`, but many other tools and "
533 "editors are not yet supported. If you end up writing a Rust mode for your "
534 "favorite editor, let us know so that we can link to it."
536 "ctags は `src/etc/ctags.rust` を利用することで rust をサポートします。多くの"
537 "ツールやエディタは rust をサポートしていません。もしあなたがお気に入りのエ"
538 "ディタ向けのRust モードを作成した場合、リンクを貼りたいのでお知らせください。"
541 #: src/doc/tutorial.md:180
543 "[sublime]: http://github.com/dbp/sublime-rust [sublime-pkg]: http://wbond."
544 "net/sublime_packages/package_control"
546 "[sublime]: http://github.com/dbp/sublime-rust\n"
547 "[sublime-pkg]: http://wbond.net/sublime_packages/package_control"
550 #: src/doc/tutorial.md:182
551 msgid "# Syntax basics"
555 #: src/doc/tutorial.md:190
557 "Assuming you've programmed in any C-family language (C++, Java, JavaScript, "
558 "C#, or PHP), Rust will feel familiar. Code is arranged in blocks delineated "
559 "by curly braces; there are control structures for branching and looping, "
560 "like the familiar `if` and `while`; function calls are written `myfunc(arg1, "
561 "arg2)`; operators are written the same and mostly have the same precedence "
562 "as in C; comments are again like C; module names are separated with double-"
563 "colon (`::`) as with C++."
565 "あなたが C 系統の言語 (C++, Java, JavaScript, C# または PHP) でプログラミング"
566 "したことがあれば、Rust の構文には馴染みがあるでしょう。コードは波括弧で区切ら"
567 "れたブロックの中に配置され、分岐やループのための制御構文はお馴染みの `if` と "
568 "`while` で、関数呼び出しは `myfunc(arg1, arg2)` と書け、演算子は C のものと同"
569 "じで、優先順位もほとんど同じで、コメントもまた C と同じで、モジュール名のセパ"
570 "レータは C++ と同様 2つのコロン (`::`) です。"
573 #: src/doc/tutorial.md:195
575 "The main surface difference to be aware of is that the condition at the head "
576 "of control structures like `if` and `while` does not require parentheses, "
577 "while their bodies *must* be wrapped in braces. Single-statement, unbraced "
578 "bodies are not allowed."
580 "表記上の違いで注目すべき点は、`if` や `while` などの制御構造の先頭にある条件"
581 "句を丸括弧で囲う必要がない点と、ボディ部をで波括弧で **囲わなければならない"
582 "** という点です。ボディ部が単一のステートメントであっても、波括弧を省略するこ"
586 #: src/doc/tutorial.md:208
590 "# mod universe { pub fn recalibrate() -> bool { true } }\n"
592 " /* A simple loop */\n"
594 " // A tricky calculation\n"
595 " if universe::recalibrate() {\n"
603 "# mod universe { pub fn recalibrate() -> bool { true } }\n"
608 " if universe::recalibrate() {\n"
616 #: src/doc/tutorial.md:212
618 "The `let` keyword introduces a local variable. Variables are immutable by "
619 "default. To introduce a local variable that you can re-assign later, use "
622 "`let` キーワードにより、ローカル変数を定義します。変数はデフォルトでは "
623 "immutable (変更不可) です。再代入可能なローカル変数を定義するためには、`let "
627 #: src/doc/tutorial.md:216
628 msgid "~~~~ let hi = \"hi\"; let mut count = 0;"
631 " let hi = \"hi\";\n"
632 " let mut count = 0;"
635 #: src/doc/tutorial.md:222
637 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
639 "while count < 10 {\n"
640 " println!(\"count is {}\", count);\n"
647 "extern crate world;\n"
648 "fn main() { println(~\"hello \" + world::explore()); }\n"
652 #: src/doc/tutorial.md:226
655 #| "Although Rust can almost always infer the types of local variables, you "
656 #| "can specify a variable's type by following it with a colon, then the type "
657 #| "name. Static items, on the other hand, always require a type annotation."
659 "Although Rust can almost always infer the types of local variables, you can "
660 "specify a variable's type by following it in the `let` with a colon, then "
661 "the type name. Static items, on the other hand, always require a type "
664 "Rust は、ほぼすべてのローカル変数の型を推論してくれますが、コロンの後に続けて"
665 "型名を書くことで、明示的に変数の型を指定することもできます。一方、静的な項目 "
666 "(static item) には、常に型注釈を指定しなければなりません。"
669 #: src/doc/tutorial.md:233
671 "~~~~ static MONSTER_FACTOR: f64 = 57.8; let monster_size = MONSTER_FACTOR * "
672 "10.0; let monster_size: int = 50; ~~~~"
675 "static MONSTER_FACTOR: f64 = 57.8;\n"
676 "let monster_size = MONSTER_FACTOR * 10.0;\n"
677 "let monster_size: int = 50;\n"
681 #: src/doc/tutorial.md:242
683 "Local variables may shadow earlier declarations, as in the previous example: "
684 "`monster_size` was first declared as a `f64`, and then a second "
685 "`monster_size` was declared as an `int`. If you were to actually compile "
686 "this example, though, the compiler would determine that the first "
687 "`monster_size` is unused and issue a warning (because this situation is "
688 "likely to indicate a programmer error). For occasions where unused variables "
689 "are intentional, their names may be prefixed with an underscore to silence "
690 "the warning, like `let _monster_size = 50;`."
692 "ローカル変数は、先行する宣言を隠すことがあります。先の例では、1つ目の "
693 "`monster_size` は `f64` として宣言され、2つ目の `monster_size` は `int` とし"
694 "て宣言されています。このプログラムをコンパイルした場合、「1つ目の "
695 "`monster_size` は未使用である」という警告メッセージが出力されます (プログラマ"
696 "が何らかの誤りを犯している可能性があるため)。未使用変数の存在が意図的なもので"
697 "あった場合、未使用変数名の先頭にアンダースコアをつけることで、警告を抑制でき"
698 "ます (`let _monster_size = 50;`)。"
701 #: src/doc/tutorial.md:248
703 "Rust identifiers start with an alphabetic character or an underscore, and "
704 "after that may contain any sequence of alphabetic characters, numbers, or "
705 "underscores. The preferred style is to write function, variable, and module "
706 "names with lowercase letters, using underscores where they help readability, "
707 "while writing types in camel case."
709 "Rust の識別子 (identifier) は、アルファベット、数字、またはアンダースコア "
710 "(`_`) から構成されますが、先頭の文字は数字以外でなければなりません。関数名、"
711 "変数名、モジュール名はスネークケース (アルファベットは小文字にし、単語間をア"
712 "ンダースコアで区切る) にし、型の名前はアッパーキャメルケースにすることが推奨"
716 #: src/doc/tutorial.md:253
720 "let my_variable = 100;\n"
721 "type MyType = int; // primitive types are _not_ camel case\n"
725 "let my_variable = 100;\n"
726 "type MyType = int; // プリミティブ型はキャメルケース __ではない__\n"
730 #: src/doc/tutorial.md:255
731 msgid "## Expressions and semicolons"
735 #: src/doc/tutorial.md:261
737 "Though it isn't apparent in all code, there is a fundamental difference "
738 "between Rust's syntax and predecessors like C. Many constructs that are "
739 "statements in C are expressions in Rust, allowing code to be more concise. "
740 "For example, you might write a piece of code like this:"
742 "コードの見た目から明らかではないのですが、Rust の構文と、C 等の先行言語の構文"
743 "との間の根本的な違いがとして、C ではステートメントとして扱われる多くの構文要"
744 "素が、Rust では式として扱われるという点があります。Rust のこの特徴により、"
745 "コードをより簡潔に書くことができるようになります。例えば、C 系言語では以下の"
749 #: src/doc/tutorial.md:275
750 msgid "But, in Rust, you don't have to repeat the name `price`:"
751 msgstr "Rust では、変数名 `price` を繰り返し書く必要はありません。"
754 #: src/doc/tutorial.md:293
756 "Both pieces of code are exactly equivalent: they assign a value to `price` "
757 "depending on the condition that holds. Note that there are no semicolons in "
758 "the blocks of the second snippet. This is important: the lack of a semicolon "
759 "after the last statement in a braced block gives the whole block the value "
760 "of that last expression."
762 "どちらのコードも、全く等価です。`price` には、条件 (この場合は `item` の値) "
763 "に対応した値が代入されます。2つ目のコード例では、ブロックの中にセミコロンが書"
764 "かれていないことに注意してください。ブロック内の最後のステートメントの後にセ"
765 "ミコロンがない場合、**ブロック全体の値 (ブロックを式として評価した際の値) は"
769 #: src/doc/tutorial.md:299
771 "Put another way, the semicolon in Rust *ignores the value of an "
772 "expression*. Thus, if the branches of the `if` had looked like `{ 4; }`, "
773 "the above example would simply assign `()` (nil or void) to `price`. But "
774 "without the semicolon, each branch has a different value, and `price` gets "
775 "the value of the branch that was taken."
777 "言い換えると、Rust では、セミコロンにより **式の値が無視されます。** したがっ"
778 "て、上記の例で `if` のすべての節 (branch) が `{ 4; }` のように書かれていたと"
779 "したら、`price` には単に `()` (nil または void の意) が代入されます。しかし、"
780 "式にセミコロンが付けられていない場合、各節はそれぞれ異なる値を持ち、 `price` "
781 "には節のうち実行されたものの値が代入されます。"
784 #: src/doc/tutorial.md:304
786 "In short, everything that's not a declaration (declarations are `let` for "
787 "variables; `fn` for functions; and any top-level named items such as [traits]"
788 "(#traits), [enum types](#enums), and static items) is an expression, "
789 "including function bodies."
791 "まとめると、宣言 (変数の `let`, 関数の `fn`, [トレイト](#トレイト) や [列挙"
792 "型](#列挙型)、静的な項目などの、トップレベルで名前を持つ項目) でないものは、"
793 "すべてが式となります。関数のボディ部も式です。"
796 #: src/doc/tutorial.md:312
800 "fn is_four(x: int) -> bool {\n"
801 " // No need for a return statement. The result of the expression\n"
802 " // is used as the return value.\n"
808 "fn is_four(x: int) -> bool {\n"
809 " // return ステートメントは省略可能。最後の式の値が戻り値となる。\n"
815 #: src/doc/tutorial.md:314
816 msgid "## Primitive types and literals"
817 msgstr "## プリミティブ型とリテラル"
820 #: src/doc/tutorial.md:321
822 "There are general signed and unsigned integer types, `int` and `uint`, as "
823 "well as 8-, 16-, 32-, and 64-bit variants, `i8`, `u16`, etc. Integers can "
824 "be written in decimal (`144`), hexadecimal (`0x90`), octal (`0o70`), or "
825 "binary (`0b10010000`) base. Each integral type has a corresponding literal "
826 "suffix that can be used to indicate the type of a literal: `i` for `int`, "
827 "`u` for `uint`, `i8` for the `i8` type."
829 "Rust には、他の言語と同様、符号付き整数の `int` と、符号なし整数の `uint` が"
830 "あります。同様に、8 または 16, 32, 64 ビット幅の `i8`, `u16` などもあります。"
831 "整数リテラルは、10進数 (`144`)、16進数 (`0x90`)、または2進数 (`0b10010000`) "
832 "の表記が可能です。整数リテラルの型を表すための、各整数型に対応したサフィック"
833 "スがあります。例えば、`i` は `int` 型、`u` は `uint` 型、`i8` は `i8` 型に対"
837 #: src/doc/tutorial.md:327
839 "In the absence of an integer literal suffix, Rust will infer the integer "
840 "type based on type annotations and function signatures in the surrounding "
841 "program. In the absence of any type information at all, Rust will assume "
842 "that an unsuffixed integer literal has type `int`."
844 "サフィックスが省略されている整数リテラルの型は、周囲にある型アノテーションや"
845 "関数のシグネチャから推論されます。型について一切情報が得られない場合、サ"
846 "フィックスなしの整数リテラルは `int` 型だとみなされます。"
849 #: src/doc/tutorial.md:334
853 #| "let a = 1; // a is an int\n"
854 #| "let b = 10i; // b is an int, due to the 'i' suffix\n"
855 #| "let c = 100u; // c is a uint\n"
856 #| "let d = 1000i32; // d is an i32\n"
860 "let a = 1; // `a` is an `int`\n"
861 "let b = 10i; // `b` is an `int`, due to the `i` suffix\n"
862 "let c = 100u; // `c` is a `uint`\n"
863 "let d = 1000i32; // `d` is an `i32`\n"
867 "let a = 1; // a は int\n"
868 "let b = 10i; // b は int (サフィックス 'i' がある)\n"
869 "let c = 100u; // c は uint\n"
870 "let d = 1000i32; // d は i32\n"
874 #: src/doc/tutorial.md:339
877 #| "There are two floating-point types: `f32`, and `f64`. Floating-point "
878 #| "numbers are written `0.0`, `1e6`, or `2.1e-4`. Like integers, floating-"
879 #| "point literals are inferred to the correct type. Suffixes `f32`, and "
880 #| "`f64` can be used to create literals of a specific type."
882 "There are two floating-point types: `f32`, and `f64`. Floating-point "
883 "numbers are written `0.0`, `1e6`, or `2.1e-4`. Like integers, floating-"
884 "point literals are inferred to the correct type. Suffixes `f32`, and `f64` "
885 "can be used to create literals of a specific type."
887 "浮動小数型は、 `f32`, `f64` の2種類があります。浮動小数リテラルは `0.0` や、 "
888 "`1e6`、 `2.1e-4` といった表記が可能です。整数と同じく、サフィックスが省略され"
889 "た浮動小数リテラルは型推論されます。浮動小数のサフィックスは `f32`、`f64` の3"
890 "種類で、リテラルの末尾につけることで、対応する型の値を作り出すことができま"
894 #: src/doc/tutorial.md:341
895 msgid "The keywords `true` and `false` produce literals of type `bool`."
896 msgstr "`true` キーワードと `false` キーワードは、`bool` 型のリテラルです。"
899 #: src/doc/tutorial.md:348
902 #| "Characters, the `char` type, are four-byte Unicode codepoints, whose "
903 #| "literals are written between single quotes, as in `'x'`. Just like C, "
904 #| "Rust understands a number of character escapes, using the backslash "
905 #| "character, such as `\\n`, `\\r`, and `\\t`. String literals, written "
906 #| "between double quotes, allow the same escape sequences. More on strings "
907 #| "[later](#vectors-and-strings)."
909 "Characters, the `char` type, are four-byte Unicode codepoints, whose "
910 "literals are written between single quotes, as in `'x'`. Just like C, Rust "
911 "understands a number of character escapes, using the backslash character, "
912 "such as `\\n`, `\\r`, and `\\t`. String literals, written between double "
913 "quotes, allow the same escape sequences, and do no other processing, unlike "
914 "languages such as PHP or shell."
916 "文字は `char` 型で表され、4バイトの Unicode コードポイントに対応しています。"
917 "`'x'` のように、文字をシングルクオートで囲んだものが文字リテラルです。C と同"
918 "様、 Rust もバックスラッシュを使ったエスケープシーケンス (`\\n`, `\\r`, `"
919 "\\t`) に対応しています。文字列リテラル (複数の文字をダブルクオートで囲ったも"
920 "の) も同じエスケープシーケンスに対応しています。詳細は [ベクタと文字列](#ベク"
924 #: src/doc/tutorial.md:356
925 msgid "The nil type, written `()`, has a single value, also written `()`."
927 "nil 型は `()` と表記されます。 nil 型の唯一の値も `()` と表記されます。"
930 #: src/doc/tutorial.md:358
935 #: src/doc/tutorial.md:363
937 "Rust's set of operators contains very few surprises. Arithmetic is done with "
938 "`*`, `/`, `%`, `+`, and `-` (multiply, quotient, remainder, add, and "
939 "subtract). `-` is also a unary prefix operator that negates numbers. As in "
940 "C, the bitwise operators `>>`, `<<`, `&`, `|`, and `^` are also supported."
942 "Rust の演算子には、あまり変わったものはありません。算術演算子は `*` (乗算)、 "
943 "`/` (除算), `%` (剰余), `+` (加算), `-` (減算) です。`-` は数の符号を反転する"
944 "単行演算子でもあります。C と同様、ビット演算子 `>>`, `<<`, `&`, `|`, `^` もサ"
948 #: src/doc/tutorial.md:366
951 #| "Note that, if applied to an integer value, `!` flips all the bits (like "
954 "Note that, if applied to an integer value, `!` flips all the bits (bitwise "
955 "NOT, like `~` in C)."
957 "整数型に対して `!` 演算子を適用すると、ビット単位での反転操作 (C の `~` と同"
958 "じ動作) が行われることに注意してください。"
961 #: src/doc/tutorial.md:370
963 "The comparison operators are the traditional `==`, `!=`, `<`, `>`, `<=`, and "
964 "`>=`. Short-circuiting (lazy) boolean operators are written `&&` (and) and "
967 "比較演算子は、従来の言語と同じく、`==`, `!=`, `<`, `>`, `<=`, `>=` です。短絡"
968 "評価 (遅延評価) されるブール演算子は、 `&&` (and 演算) と `||` (or 演算) があ"
972 #: src/doc/tutorial.md:377
975 #| "For type casting, Rust uses the binary `as` operator. It takes an "
976 #| "expression on the left side and a type on the right side and will, if a "
977 #| "meaningful conversion exists, convert the result of the expression to the "
980 "For compile-time type casting, Rust uses the binary `as` operator. It takes "
981 "an expression on the left side and a type on the right side and will, if a "
982 "meaningful conversion exists, convert the result of the expression to the "
983 "given type. Generally, `as` is only used with the primitive numeric types or "
984 "pointers, and is not overloadable. [`transmute`][transmute] can be used for "
985 "unsafe C-like casting of same-sized types."
987 "Rust では、型のキャストには二項演算子 `as` を使います。`as` 演算子は、左辺に"
988 "式、右辺に型を取り、意味のある変換が可能な場合に限り、式の評価結果を指定され"
992 #: src/doc/tutorial.md:383
993 msgid "~~~~ let x: f64 = 4.0; let y: uint = x as uint; assert!(y == 4u); ~~~~"
996 "let x: f64 = 4.0;\n"
997 "let y: uint = x as uint;\n"
998 "assert!(y == 4u);\n"
1002 #: src/doc/tutorial.md:385
1005 #| "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz [win-exe]: "
1006 #| "http://static.rust-lang.org/dist/rust-nightly-install.exe"
1008 "[transmute]: http://static.rust-lang.org/doc/master/std/cast/fn.transmute."
1011 "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz\n"
1012 "[win-exe]: http://static.rust-lang.org/dist/rust-nightly-install.exe"
1015 #: src/doc/tutorial.md:387
1016 msgid "## Syntax extensions"
1020 #: src/doc/tutorial.md:395
1023 #| "*Syntax extensions* are special forms that are not built into the language,\n"
1024 #| "but are instead provided by the libraries. To make it clear to the reader when\n"
1025 #| "a name refers to a syntax extension, the names of all syntax extensions end\n"
1026 #| "with `!`. The standard library defines a few syntax extensions, the most\n"
1027 #| "useful of which is `fmt!`, a `sprintf`-style text formatter that you will\n"
1028 #| "often see in examples.\n"
1030 "*Syntax extensions* are special forms that are not built into the language,\n"
1031 "but are instead provided by the libraries. To make it clear to the reader when\n"
1032 "a name refers to a syntax extension, the names of all syntax extensions end\n"
1033 "with `!`. The standard library defines a few syntax extensions, the most\n"
1034 "useful of which is [`format!`][fmt], a `sprintf`-like text formatter that you\n"
1035 "will often see in examples, and its related family of macros: `print!`,\n"
1036 "`println!`, and `write!`.\n"
1037 msgstr "**構文拡張** *(Syntax extensions)* は言語組み込みではなく、ライブラリにより提供される特殊形式 (スペシャルフォーム) です。プログラム中に構文拡張が登場したことを外見上明確にするため、構文拡張の名前には末尾に `!` をつけますなければなりません。標準ライブラリではいくつかの構文拡張が定義されていますが、その中でも最も有用なのは `fmt!` です。`fmt!` は `sprintf`スタイルの書式整形を行うもので、後のコード例の中でも何度か見かけることになるでしょう。\n"
1040 #: src/doc/tutorial.md:399
1043 #| "`fmt!` supports most of the directives that [printf][pf] supports, but "
1044 #| "unlike printf, will give you a compile-time error when the types of the "
1045 #| "directives don't match the types of the arguments."
1047 "`format!` draws syntax from Python, but contains many of the same principles "
1048 "that [printf][pf] has. Unlike printf, `format!` will give you a compile-time "
1049 "error when the types of the directives don't match the types of the "
1052 "`fmt!` は、[`printf`][pf] がサポートするほとんどのディレクティブ (フォーマッ"
1053 "ト指定子) をサポートしますが、`printf` とは異なり、ディレクティブと引数の型が"
1054 "一致しない場合はコンパイルエラーとなります。"
1057 #: src/doc/tutorial.md:402
1058 msgid "~~~~ # let mystery_object = ();"
1061 "# let mystery_object = ();"
1064 #: src/doc/tutorial.md:409
1067 #| "// %? will conveniently print any type println(fmt!(\"what is this thing: "
1068 #| "%?\", mystery_object)); ~~~~"
1070 "// `{:?}` will conveniently print any type println!(\"what is this thing: "
1071 "{:?}\", mystery_object); ~~~~"
1073 "// %? は任意の型を表示できる便利なディレクティブです\n"
1074 "println(fmt!(\"what is this thing: %?\", mystery_object));\n"
1078 #: src/doc/tutorial.md:412
1081 #| "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz [win-exe]: "
1082 #| "http://static.rust-lang.org/dist/rust-nightly-install.exe"
1084 "[pf]: http://en.cppreference.com/w/cpp/io/c/fprintf [fmt]: http://static."
1085 "rust-lang.org/doc/master/std/fmt/index.html"
1087 "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz\n"
1088 "[win-exe]: http://static.rust-lang.org/dist/rust-nightly-install.exe"
1091 #: src/doc/tutorial.md:416
1094 #| "You can define your own syntax extensions with the macro system. For "
1095 #| "details, see the [macro tutorial][macros]."
1097 "You can define your own syntax extensions with the macro system. For "
1098 "details, see the [macro tutorial][macros]. Note that macro definition is "
1099 "currently considered an unstable feature."
1101 "マクロシステムを利用すれば、独自に構文拡張を定義することも可能です。詳細は "
1102 "[マクロチュートリアル][macros] を参照してください。"
1105 #: src/doc/tutorial.md:418
1106 msgid "# Control structures"
1110 #: src/doc/tutorial.md:420
1111 msgid "## Conditionals"
1115 #: src/doc/tutorial.md:424
1117 "We've seen `if` expressions a few times already. To recap, braces are "
1118 "compulsory, an `if` can have an optional `else` clause, and multiple `if`/"
1119 "`else` constructs can be chained together:"
1121 "`if` 式は既に何度か登場しています。これまでの説明の要点をまとめると、波括弧は"
1122 "省略不可であり、`if` 式には省略可能な `else` 句があり、複数個の `if`/`else` "
1123 "構文は互いに連鎖できる、ということでした。"
1126 #: src/doc/tutorial.md:439
1128 "The condition given to an `if` construct *must* be of type `bool` (no "
1129 "implicit conversion happens). If the arms are blocks that have a value, this "
1130 "value must be of the same type for every arm in which control reaches the "
1133 "`if` 構文の条件式は `bool` 型ででなければなりません (暗黙的な型変換は行われま"
1134 "せん)。同一の `if` 構文に属する arm のうち、制御がブロックの末尾まで到達し得"
1135 "るものは、すべて同じ型の値でなければなりません。"
1138 #: src/doc/tutorial.md:447
1140 #| msgid "~~~~ fn line(a: int, b: int, x: int) -> int { a * x + b } fn oops(a: int, b: int, x: int) -> () { a * x + b; }"
1143 "fn signum(x: int) -> int {\n"
1144 " if x < 0 { -1 }\n"
1145 " else if x > 0 { 1 }\n"
1151 "fn line(a: int, b: int, x: int) -> int { a * x + b }\n"
1152 "fn oops(a: int, b: int, x: int) -> () { a * x + b; }"
1155 #: src/doc/tutorial.md:449
1156 msgid "## Pattern matching"
1160 #: src/doc/tutorial.md:455
1162 "Rust's `match` construct is a generalized, cleaned-up version of C's "
1163 "`switch` construct. You provide it with a value and a number of *arms*, each "
1164 "labelled with a pattern, and the code compares the value against each "
1165 "pattern in order until one matches. The matching pattern executes its "
1166 "corresponding arm."
1168 "Rust の `match` 構文は、C の `switch` 構文を整理・一般化したものです。"
1169 "`match` 構文は、パターンマッチの対象となる値と、いくつかの、 **arm** (**訳"
1170 "注:** 分岐条件と、条件を満たした場合に実行される式の組のこと。`match` の場合"
1171 "はパターンと式の組) から構成されます。`match` 構文では、指定された値とパター"
1172 "ンの比較をコード内で記述された順番通りに行います。比較の結果、値とパターンが"
1173 "マッチした場合、対応する式が実行されます。マッチしたパターンの後に現れるパ"
1177 #: src/doc/tutorial.md:469
1179 "Unlike in C, there is no \"falling through\" between arms: only one arm "
1180 "executes, and it doesn't have to explicitly `break` out of the construct "
1181 "when it is finished."
1183 "C とは異なり、Rust では arm 間での「フォールスルー」はないため、1つの arm の"
1184 "みが実行されます。 そのため、 `match` 構文から抜ける箇所を `break` により明示"
1188 #: src/doc/tutorial.md:478
1191 #| "A `match` arm consists of a *pattern*, then an arrow `=>`, followed by an "
1192 #| "*action* (expression). Literals are valid patterns and match only their "
1193 #| "own value. A single arm may match multiple different patterns by "
1194 #| "combining them with the pipe operator (`|`), so long as every pattern "
1195 #| "binds the same set of variables. Ranges of numeric literal patterns can "
1196 #| "be expressed with two dots, as in `M..N`. The underscore (`_`) is a "
1197 #| "wildcard pattern that matches any single value. The asterisk (`*`) is a "
1198 #| "different wildcard that can match one or more fields in an `enum` variant."
1200 "A `match` arm consists of a *pattern*, then an arrow `=>`, followed by an "
1201 "*action* (expression). Literals are valid patterns and match only their own "
1202 "value. A single arm may match multiple different patterns by combining them "
1203 "with the pipe operator (`|`), so long as every pattern binds the same set of "
1204 "variables. Ranges of numeric literal patterns can be expressed with two "
1205 "dots, as in `M..N`. The underscore (`_`) is a wildcard pattern that matches "
1206 "any single value. (`..`) is a different wildcard that can match one or more "
1207 "fields in an `enum` variant."
1209 "`match` の arm は、 **パターン** および矢印 `=>` と、それらに続く **アクショ"
1210 "ン** (式) から構成されます。リテラルはパターンとして利用可能で、リテラル自身"
1211 "の値のみとマッチするパターンを意味します。複数のパターンをパイプ演算子 (`|`) "
1212 "で結合させることで、複数の異なるパターンにマッチする単一の arm を作ることがで"
1213 "きます。ただし、変数の束縛を行うパターンをパイプ演算子で結合する場合は、すべ"
1214 "てのパータンで束縛する変数の名前・数が一致している必要があります (**訳注:** "
1215 "変数の束縛については、本節の後半で説明します)。指定した範囲に含まれる任意の数"
1216 "値にマッチするパターンは、2つのドットで `M..N` のように表記します。アンダース"
1217 "コア (`_`) は任意の値1つにマッチするワイルドカードパターンです。アスタリスク "
1218 "(`*`) は任意の値をもつ、`enum` バリアントの複数のフィールドにマッチする、ワイ"
1219 "ルドカードです (**訳注** `enum` については、[列挙型](#列挙型) の節で説明しま"
1223 #: src/doc/tutorial.md:483
1225 "The patterns in a match arm are followed by a fat arrow, `=>`, then an "
1226 "expression to evaluate. Each case is separated by commas. It's often "
1227 "convenient to use a block expression for each case, in which case the commas "
1230 "`match` の各 arm はカンマ (`,`) は区切ります。 arm のアクションとしてブロック"
1231 "を記述することも可能で、その場合は arm 末尾のカンマを省略することが可能です。"
1234 #: src/doc/tutorial.md:496
1236 "`match` constructs must be *exhaustive*: they must have an arm covering "
1237 "every possible case. For example, the typechecker would reject the previous "
1238 "example if the arm with the wildcard pattern was omitted."
1240 "`match` 構文は **網羅的** でなければなりません。すなわち、`match` 対象の値と"
1241 "同じ型を持つ任意の値に対し、少なくとも 1 つ以上のパターンがマッチするだけの "
1242 "arm を含んでいなければなりません。例えば、上記コード例のワイルドカードパター"
1243 "ンの arm を削除した場合、型検査によりコンパイルエラーとされます。"
1246 #: src/doc/tutorial.md:500
1248 "A powerful application of pattern matching is *destructuring*: matching in "
1249 "order to bind names to the contents of data types."
1251 "パターンマッチの応用として、 **destructuring** という強力な機能があります。構"
1252 "造化代入とは、データ型内部の値を名前に束縛 (bind) することです。"
1255 #: src/doc/tutorial.md:504
1257 "> *Note:* The following code makes use of tuples (`(f64, f64)`) which > "
1258 "are explained in section 5.3. For now you can think of tuples as a list of > "
1261 "> *注意:* 以下のコード例では5.3 節で説明されるタプル (`(f64, f64)`) を"
1262 "使っています。現時点では、タプルは項目のリストのようなものだとみなしてくださ"
1266 #: src/doc/tutorial.md:525
1269 #| "A variable name in a pattern matches any value, *and* binds that name to "
1270 #| "the value of the matched value inside of the arm's action. Thus, `(0f, "
1271 #| "y)` matches any tuple whose first element is zero, and binds `y` to the "
1272 #| "second element. `(x, y)` matches any two-element tuple, and binds both "
1273 #| "elements to variables."
1275 "A variable name in a pattern matches any value, *and* binds that name to the "
1276 "value of the matched value inside of the arm's action. Thus, `(0.0, y)` "
1277 "matches any tuple whose first element is zero, and binds `y` to the second "
1278 "element. `(x, y)` matches any two-element tuple, and binds both elements to "
1279 "variables. A subpattern can also be bound to a variable, using `variable @ "
1280 "pattern`. For example:"
1282 "パターン内に登場する変数は、任意の値にマッチ **するだけでなく**、arm のアク"
1283 "ション内ではマッチした値が変数の名前に束縛されます。従って、 `(0f, y)` は1つ"
1284 "目の要素が 0 である任意のタプルにマッチし、 `y` には2つ目の要素の値が束縛され"
1285 "ます。`(x, y)` は任意の2要素のタプルにマッチし、タプルの各要素がそれぞれの変"
1289 #: src/doc/tutorial.md:540
1291 "Any `match` arm can have a guard clause (written `if EXPR`), called a "
1292 "*pattern guard*, which is an expression of type `bool` that determines, "
1293 "after the pattern is found to match, whether the arm is taken or not. The "
1294 "variables bound by the pattern are in scope in this guard expression. The "
1295 "first arm in the `angle` example shows an example of a pattern guard."
1297 "`match` の任意の arm は **パターンガード** と呼ばれる、ガード節 (`if EXPR` と"
1298 "書かれる) を持つことができます。パターンガードは、 `bool` 型の値をもつ式で、"
1299 "値にマッチするパターンが見つかった後に評価され、arm が実行されるかどうかを決"
1300 "定します。パターンにより束縛される変数は、ガード式からも参照可能 (ガード式の"
1301 "スコープに含まれる) です。`angle` 関数の例の最初の arm に、パターンガードの使"
1305 #: src/doc/tutorial.md:545
1307 "You've already seen simple `let` bindings, but `let` is a little fancier "
1308 "than you've been led to believe. It, too, supports destructuring patterns. "
1309 "For example, you can write this to extract the fields from a tuple, "
1310 "introducing two variables at once: `a` and `b`."
1312 "`let` は単純な変数束縛だけではなく、パターンによる destructuring をサポートし"
1313 "ています。例えば、タプルからフィールドを抽出し、1度に2つの変数 `a` と `b` を"
1317 #: src/doc/tutorial.md:550
1319 "~~~~ # fn get_tuple_of_two_ints() -> (int, int) { (1, 1) } let (a, b) = "
1320 "get_tuple_of_two_ints(); ~~~~"
1323 "# fn get_tuple_of_two_ints() -> (int, int) { (1, 1) }\n"
1324 "let (a, b) = get_tuple_of_two_ints();\n"
1328 #: src/doc/tutorial.md:554
1330 "Let bindings only work with _irrefutable_ patterns: that is, patterns that "
1331 "can never fail to match. This excludes `let` from matching literals and most "
1334 "let による変数定義が可能なのは、パターンが __不可反駁__ (iefutable; 必ず値に"
1335 "マッチする) な場合のみです。この制限により、`let` を用いた destructuring で、"
1336 "リテラルやほとんどの `enum` バリアントをパターンとして用いることはできませ"
1340 #: src/doc/tutorial.md:556
1345 #: src/doc/tutorial.md:561
1348 #| "`while` denotes a loop that iterates as long as its given condition "
1349 #| "(which must have type `bool`) evaluates to `true`. Inside a loop, the "
1350 #| "keyword `break` aborts the loop, and `loop` aborts the current iteration "
1351 #| "and continues with the next."
1353 "`while` denotes a loop that iterates as long as its given condition (which "
1354 "must have type `bool`) evaluates to `true`. Inside a loop, the keyword "
1355 "`break` aborts the loop, and `continue` aborts the current iteration and "
1356 "continues with the next."
1358 "`while` は、与えられた条件 (`bool` 型の値でなければならない) が `true` と評価"
1359 "されている間、繰り返し実行されるループを表します。`break` キーワードは、ルー"
1360 "プの実行を中断させます。`loop` キーワードは、現在実行中の繰り返し (イテレー"
1361 "ション) を中断し、次の繰り返しを実行します。"
1364 #: src/doc/tutorial.md:570
1366 "`loop` denotes an infinite loop, and is the preferred way of writing `while "
1369 "`loop` は無限ループを表します。 `while true` と書く代わりに `loop` と書くこと"
1373 #: src/doc/tutorial.md:582
1375 "This code prints out a weird sequence of numbers and stops as soon as it "
1376 "finds one that can be divided by five."
1377 msgstr "このコードは 5 で割り切れる数値が現れるまで、数値を出力し続けます。"
1380 #: src/doc/tutorial.md:584
1381 msgid "# Data structures"
1385 #: src/doc/tutorial.md:586
1390 #: src/doc/tutorial.md:591
1392 "Rust struct types must be declared before they are used using the `struct` "
1393 "syntax: `struct Name { field1: T1, field2: T2 [, ...] }`, where `T1`, "
1394 "`T2`, ... denote types. To construct a struct, use the same syntax, but "
1395 "leave off the `struct`: for example: `Point { x: 1.0, y: 2.0 }`."
1397 "Rust の構造体 (struct) 型を利用するためには、`struct Name { field1: T1, "
1398 "field2: T2 [, ...] }` (`T1`, `T2`, .. は型を意味する) のように、 `struct` 構"
1399 "文を使った型の宣言が必要です。構造体型の値は、`Point { x: 1.0, y: 2.0 }` のよ"
1400 "うに、構造体宣言から `struct` を取り除いたものと同じ構文で作成します。"
1403 #: src/doc/tutorial.md:595
1405 "Structs are quite similar to C structs and are even laid out the same way in "
1406 "memory (so you can read from a Rust struct in C, and vice-versa). Use the "
1407 "dot operator to access struct fields, as in `mypoint.x`."
1409 "Rust の構造体は C の構造体とよく似ており、メモリ上のレイアウトも C と同じで"
1410 "す (そのため、Rust と C で相互に構造体を読み込むことが可能です)。構造体の"
1411 "フィールドにアクセスするには、 `mypoint.x` のように、ドット演算子を用います。"
1414 #: src/doc/tutorial.md:602
1416 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
1427 "extern crate world;\n"
1428 "fn main() { println(~\"hello \" + world::explore()); }\n"
1432 #: src/doc/tutorial.md:605
1435 #| "Inherited mutability means that any field of a struct may be mutable, if "
1436 #| "the struct is in a mutable slot (or a field of a struct in a mutable "
1437 #| "slot, and so forth)."
1439 "Structs have \"inherited mutability\", which means that any field of a "
1440 "struct may be mutable, if the struct is in a mutable slot."
1442 "継承ミュータビリティ (inherited mutability) とは、構造体自体がミュータブルな"
1443 "スロットに配置されている場合、構造体のすべてのフィールドもミュータブルになる"
1444 "という性質を意味する言葉です (ミュータブルなスロットに配置された構造体の"
1445 "フィールドに配置された構造体のフィールドもまた、ミュータブルになります) 。"
1448 #: src/doc/tutorial.md:609
1450 "With a value (say, `mypoint`) of such a type in a mutable location, you can "
1451 "do `mypoint.y += 1.0`. But in an immutable location, such an assignment to a "
1452 "struct without inherited mutability would result in a type error."
1454 "ある構造体の値 (仮に `mypoint` とします) がミュータブルな場所に配置された場"
1455 "合、 `mypoint.y += 1.0` というような操作を行うことができます。しかし、構造体"
1456 "がイミュータブルな場所に配置された場合、ミュータビリティは継承されないため、"
1457 "このような代入操作は型エラーとなります。"
1460 #: src/doc/tutorial.md:614
1462 "~~~~ {.ignore} # struct Point { x: f64, y: f64 } let mut mypoint = Point "
1463 "{ x: 1.0, y: 1.0 }; let origin = Point { x: 0.0, y: 0.0 };"
1466 "# struct Point { x: f64, y: f64 }\n"
1467 "let mut mypoint = Point { x: 1.0, y: 1.0 };\n"
1468 "let origin = Point { x: 0.0, y: 0.0 };"
1471 #: src/doc/tutorial.md:618
1474 #| "mypoint.y += 1.0; // mypoint is mutable, and its fields as well origin.y "
1475 #| "+= 1.0; // ERROR: assigning to immutable field ~~~~"
1477 "mypoint.y += 1.0; // `mypoint` is mutable, and its fields as well origin.y "
1478 "+= 1.0; // ERROR: assigning to immutable field ~~~~"
1480 "mypoint.y += 1.0;\n"
1481 "// mypoint はミュータブル\n"
1482 "origin.y += 1.0; // ERROR: assigning to immutable field\n"
1486 #: src/doc/tutorial.md:621
1488 "`match` patterns destructure structs. The basic syntax is `Name { fieldname: "
1491 "`match` のパターンで構造体の destructuring を行うことも可能です。パターン"
1492 "は、 `Name { fieldname: pattern, ... }` という構文を持ちます。"
1495 #: src/doc/tutorial.md:630
1497 #| msgid "~~~ # struct Point { x: f64, y: f64 } let point = &@~Point { x: 10f, y: 20f }; println(fmt!(\"%f\", point.x)); ~~~"
1500 "# struct Point { x: f64, y: f64 }\n"
1501 "# let mypoint = Point { x: 0.0, y: 0.0 };\n"
1503 " Point { x: 0.0, y: yy } => println!(\"{}\", yy),\n"
1504 " Point { x: xx, y: yy } => println!(\"{} {}\", xx, yy),\n"
1509 "# struct Point { x: f64, y: f64 }\n"
1510 "let point = &@~Point { x: 10f, y: 20f };\n"
1511 "println(fmt!(\"%f\", point.x));\n"
1515 #: src/doc/tutorial.md:637
1518 #| "In general, the field names of a struct do not have to appear in the same "
1519 #| "order they appear in the type. When you are not interested in all the "
1520 #| "fields of a struct, a struct pattern may end with `, _` (as in `Name "
1521 #| "{ field1, _ }`) to indicate that you're ignoring all other fields. "
1522 #| "Additionally, struct fields have a shorthand matching form that simply "
1523 #| "reuses the field name as the binding name."
1525 "In general, the field names of a struct do not have to appear in the same "
1526 "order they appear in the type. When you are not interested in all the fields "
1527 "of a struct, a struct pattern may end with `, ..` (as in `Name { field1, .. }"
1528 "`) to indicate that you're ignoring all other fields. Additionally, struct "
1529 "fields have a shorthand matching form that simply reuses the field name as "
1532 "構造体のフィールド名の並び順は、型定義での並び順と同じにする必要はありませ"
1533 "ん。構造体のパターンマッチで、すべてのフィールドについて考える必要がない場合"
1534 "は、`Name { field1, _ }` のように、末尾に `, _` をつけることで、他のすべての"
1535 "フィールドを無視することができます。"
1538 #: src/doc/tutorial.md:645
1540 #| msgid "~~~ # struct Point { x: f64, y: f64 } let point = &@~Point { x: 10f, y: 20f }; println(fmt!(\"%f\", point.x)); ~~~"
1543 "# struct Point { x: f64, y: f64 }\n"
1544 "# let mypoint = Point { x: 0.0, y: 0.0 };\n"
1546 " Point { x, .. } => println!(\"{}\", x),\n"
1551 "# struct Point { x: f64, y: f64 }\n"
1552 "let point = &@~Point { x: 10f, y: 20f };\n"
1553 "println(fmt!(\"%f\", point.x));\n"
1557 #: src/doc/tutorial.md:647
1562 #: src/doc/tutorial.md:650
1565 #| "Enums are datatypes that have several alternate representations. For "
1566 #| "example, consider the type shown earlier:"
1568 "Enums are datatypes that have several alternate representations. For "
1569 "example, consider the following type:"
1571 "列挙型 (enum) は、いくつかの代替表現を持つデータ型です。例えば、以下の型につ"
1575 #: src/doc/tutorial.md:664
1578 #| "A value of this type is either a `Circle`, in which case it contains a "
1579 #| "`Point` struct and a `f64`, or a `Rectangle`, in which case it contains "
1580 #| "two `Point` structs. The run-time representation of such a value includes "
1581 #| "an identifier of the actual form that it holds, much like the \"tagged "
1582 #| "union\" pattern in C, but with better static guarantees."
1584 "A value of this type is either a `Circle`, in which case it contains a "
1585 "`Point` struct and a f64, or a `Rectangle`, in which case it contains two "
1586 "`Point` structs. The run-time representation of such a value includes an "
1587 "identifier of the actual form that it holds, much like the \"tagged union\" "
1588 "pattern in C, but with better static guarantees."
1590 "この型の値は、`Point` 構造体と `f64` を含む `Circle` か、2つの `Point` 構造体"
1591 "を含む `Rectangle` のどちらかになります。列挙型の値の実行時表現には、値がどの"
1592 "形式をとっているのか示す識別子が含まれます。この表現方法は C の \"タグ付き共"
1593 "用体\" と非常によく似ていますが、Rust の列挙型はコンパイラにより値の正当性が"
1594 "静的に保証されるという点でより良いものとなっています。"
1597 #: src/doc/tutorial.md:670
1600 #| "The above declaration will define a type `Shape` that can refer to such "
1601 #| "shapes, and two functions, `Circle` and `Rectangle`, which can be used to "
1602 #| "construct values of the type (taking arguments of the specified types). "
1603 #| "So `Circle(Point { x: 0f, y: 0f }, 10f)` is the way to create a new "
1606 "The above declaration will define a type `Shape` that can refer to such "
1607 "shapes, and two functions, `Circle` and `Rectangle`, which can be used to "
1608 "construct values of the type (taking arguments of the specified types). So "
1609 "`Circle(Point { x: 0.0, y: 0.0 }, 10.0)` is the way to create a new circle."
1611 "上記の宣言は `Shape` 型と `Circle` と `Rectangle` という2つの関数も同時に定義"
1612 "します。これらの関数により、関数名に対応した `Shape` 型の値を構築することがで"
1613 "きます (引数の型も列挙型宣言時に指定されたものをとります)。例えば、"
1614 "`Circle(Point { x: 0f, y: 0f }, 10f)` という記述により、新しい circle を作る"
1618 #: src/doc/tutorial.md:673
1620 "Enum variants need not have parameters. This `enum` declaration, for "
1621 "example, is equivalent to a C enum:"
1623 "列挙型のバリアントはパラメータを省略することもできます。以下の例の `enum` 宣"
1627 #: src/doc/tutorial.md:685
1629 "This declaration defines `North`, `East`, `South`, and `West` as constants, "
1630 "all of which have type `Direction`."
1632 "この宣言は `Direction` 型の定数 `North`, `East`, `South`, `West` を定義しま"
1636 #: src/doc/tutorial.md:689
1638 "When an enum is C-like (that is, when none of the variants have parameters), "
1639 "it is possible to explicitly set the discriminator values to a constant "
1642 "列挙型が Cライクな (すべてのバリアントがパラメータを持たない) 場合、各定数の"
1643 "識別値 (discriminator value) を明示的に指定することも可能です。"
1646 #: src/doc/tutorial.md:702
1648 "If an explicit discriminator is not specified for a variant, the value "
1649 "defaults to the value of the previous variant plus one. If the first variant "
1650 "does not have a discriminator, it defaults to 0. For example, the value of "
1651 "`North` is 0, `East` is 1, `South` is 2, and `West` is 3."
1653 "バリアントの識別値が明に指定されていない場合、1つ前のバリアントの値に1を足し"
1654 "たものが設定されます。また、最初のバリアントの場合は0が設定されます。上記の例"
1655 "で言うと、 `North` は 0、 `East` は 1、 `South` は 2、`West` は3となります。"
1658 #: src/doc/tutorial.md:705
1660 "When an enum is C-like, you can apply the `as` cast operator to convert it "
1661 "to its discriminator value as an `int`."
1663 "C ライクな列挙型の値は、キャスト演算子 `as` を適用することで、`int` 型の識別"
1667 #: src/doc/tutorial.md:709
1669 "For enum types with multiple variants, destructuring is the only way to get "
1670 "at their contents. All variant constructors can be used as patterns, as in "
1671 "this definition of `area`:"
1673 "複数のバリアントを持つ列挙型で、列挙型値の内部のデータを取得するには、 "
1674 "destructuring を使う必要があります。以下の `area` 関数の例のように、バリアン"
1675 "トのコンストラクタは、パターンとして用いることもできます。"
1678 #: src/doc/tutorial.md:726
1681 #| "You can write a lone `_` to ignore an individual field, and can ignore "
1682 #| "all fields of a variant like: `Circle(*)`. As in their introduction form, "
1683 #| "nullary enum patterns are written without parentheses."
1685 "You can write a lone `_` to ignore an individual field, and can ignore all "
1686 "fields of a variant like: `Circle(..)`. As in their introduction form, "
1687 "nullary enum patterns are written without parentheses."
1689 "パターンマッチの際に、特定のフィールドに対するマッチングが不要な場合、無視し"
1690 "たいフィールドの場所に `_` を書くことで、個々のフィールドを無視することができ"
1691 "ます。また、 `Circle(*)` のように書くことで、すべてのフィールドを無視すること"
1692 "も可能です。引数をとらない列挙型のパターンは、定義時の形式と同様、丸括弧の無"
1696 #: src/doc/tutorial.md:741
1697 msgid "Enum variants may also be structs. For example:"
1698 msgstr "以下の例のように、列挙型バリアントを構造体にすることも可能です。"
1701 #: src/doc/tutorial.md:765
1706 #: src/doc/tutorial.md:770
1709 #| "Tuples in Rust behave exactly like structs, except that their fields do "
1710 #| "not have names. Thus, you cannot access their fields with dot notation. "
1711 #| "Tuples can have any arity except for 0 (though you may consider unit, "
1712 #| "`()`, as the empty tuple if you like)."
1714 "Tuples in Rust behave exactly like structs, except that their fields do not "
1715 "have names. Thus, you cannot access their fields with dot notation. Tuples "
1716 "can have any arity (number of elements) except for 0 (though you may "
1717 "consider unit, `()`, as the empty tuple if you like)."
1719 "Rust のタプルは、フィールドが名前を持たないという点以外は、構造体と同じように"
1720 "振る舞います。フィールド名が存在しないため、ドット記法を使ってタプルのフィー"
1721 "ルドにアクセスすることはできません。タプルは 1 個以上の要素を持つことができま"
1722 "す (必要ならば、ユニット型 `()` を 0 要素のタプルと見なすことはできます。)"
1725 #: src/doc/tutorial.md:777
1730 #| "struct MyTup(int, int, f64);\n"
1731 #| "let mytup: MyTup = MyTup(10, 20, 30.0);\n"
1732 #| "match mytup {\n"
1733 #| " MyTup(a, b, c) => info!(a + b + (c as int))\n"
1738 "let mytup: (int, int, f64) = (10, 20, 30.0);\n"
1740 " (a, b, c) => info!(\"{}\", a + b + (c as int))\n"
1745 "struct MyTup(int, int, f64);\n"
1746 "let mytup: MyTup = MyTup(10, 20, 30.0);\n"
1748 " MyTup(a, b, c) => info!(a + b + (c as int))\n"
1753 #: src/doc/tutorial.md:779
1754 msgid "## Tuple structs"
1758 #: src/doc/tutorial.md:784
1760 "Rust also has _tuple structs_, which behave like both structs and tuples, "
1761 "except that, unlike tuples, tuple structs have names (so `Foo(1, 2)` has a "
1762 "different type from `Bar(1, 2)`), and tuple structs' _fields_ do not have "
1765 "Rust には __タプル構造体__ と呼ばれる、構造体とタプルの両者の特徴を併せ持つも"
1766 "のが存在します。タプルと異なり、タプル構造体自体は名前を持ちます (従って、"
1767 "`Foo(1, 2)` と `Bar(1, 2)` は異なる型になります。) しかし、タプル構造体の __"
1768 "フィールド__ は名前を持ちません。"
1771 #: src/doc/tutorial.md:794
1776 #| "struct MyTup(int, int, f64);\n"
1777 #| "let mytup: MyTup = MyTup(10, 20, 30.0);\n"
1778 #| "match mytup {\n"
1779 #| " MyTup(a, b, c) => info!(a + b + (c as int))\n"
1784 "struct MyTup(int, int, f64);\n"
1785 "let mytup: MyTup = MyTup(10, 20, 30.0);\n"
1787 " MyTup(a, b, c) => info!(\"{}\", a + b + (c as int))\n"
1792 "struct MyTup(int, int, f64);\n"
1793 "let mytup: MyTup = MyTup(10, 20, 30.0);\n"
1795 " MyTup(a, b, c) => info!(a + b + (c as int))\n"
1800 #: src/doc/tutorial.md:801
1802 "There is a special case for tuple structs with a single field, which are "
1803 "sometimes called \"newtypes\" (after Haskell's \"newtype\" feature). These "
1804 "are used to define new types in such a way that the new name is not just a "
1805 "synonym for an existing type but is rather its own distinct type."
1807 "タプル構造体がフィールドを 1 つしか持たない場合、 \"newtype\" と呼ばれること"
1808 "があります (Haskell の \"newtype\" 機能に由来しています)。 このようなタプル構"
1809 "造体を使うことで、既存の型の別名 (シノニム) となる新しい型を定義することがで"
1810 "きます。この新しい型は、元にした型とは異なった型として扱われます。"
1813 #: src/doc/tutorial.md:805
1814 msgid "~~~~ struct GizmoId(int); ~~~~"
1817 "struct GizmoId(int);\n"
1821 #: src/doc/tutorial.md:808
1824 #| "Types like this can be useful to differentiate between data that have the "
1825 #| "same type but must be used in different ways."
1827 "Types like this can be useful to differentiate between data that have the "
1828 "same underlying type but must be used in different ways."
1830 "このような型は、型は同一でも、それぞれ全く異なる扱い方をしなければならない"
1831 "データを扱う場合に用いると便利です。"
1834 #: src/doc/tutorial.md:813
1835 msgid "~~~~ struct Inches(int); struct Centimeters(int); ~~~~"
1838 "struct Inches(int);\n"
1839 "struct Centimeters(int);\n"
1843 #: src/doc/tutorial.md:817
1846 #| "The above definitions allow for a simple way for programs to avoid "
1847 #| "confusing numbers that correspond to different units."
1849 "The above definitions allow for a simple way for programs to avoid confusing "
1850 "numbers that correspond to different units. Their integer values can be "
1851 "extracted with pattern matching:"
1853 "上記のような単純な定義により、異なる単位を持つ数値を混同することなく扱うこと"
1857 #: src/doc/tutorial.md:820
1859 #| msgid "~~~~ struct GizmoId(int); ~~~~"
1860 msgid "~~~ # struct Inches(int);"
1863 "struct GizmoId(int);\n"
1867 #: src/doc/tutorial.md:827
1872 #: src/doc/tutorial.md:835
1875 #| "We've already seen several function definitions. Like all other static "
1876 #| "declarations, such as `type`, functions can be declared both at the top "
1877 #| "level and inside other functions (or in modules, which we'll come back to "
1878 #| "[later](#modules-and-crates)). The `fn` keyword introduces a function. A "
1879 #| "function has an argument list, which is a parenthesized list of `expr: "
1880 #| "type` pairs separated by commas. An arrow `->` separates the argument "
1881 #| "list and the function's return type."
1883 "We've already seen several function definitions. Like all other static "
1884 "declarations, such as `type`, functions can be declared both at the top "
1885 "level and inside other functions (or in modules, which we'll come back to "
1886 "[later](#crates-and-the-module-system)). The `fn` keyword introduces a "
1887 "function. A function has an argument list, which is a parenthesized list of "
1888 "`name: type` pairs separated by commas. An arrow `->` separates the argument "
1889 "list and the function's return type."
1891 "関数定義はこれまでに何度か登場しています。他の静的な宣言 (`type` など)と同様"
1892 "に、関数はトップレベルまたは、他の関数の内部、モジュールの内部で定義すること"
1893 "ができます (モジュールについては、[後述](#モジュールとクレート)します) 。"
1896 #: src/doc/tutorial.md:841
1898 #| msgid "~~~~ fn line(a: int, b: int, x: int) -> int { a * x + b } fn oops(a: int, b: int, x: int) -> () { a * x + b; }"
1901 "fn line(a: int, b: int, x: int) -> int {\n"
1902 " return a * x + b;\n"
1907 "fn line(a: int, b: int, x: int) -> int { a * x + b }\n"
1908 "fn oops(a: int, b: int, x: int) -> () { a * x + b; }"
1911 #: src/doc/tutorial.md:846
1913 "The `return` keyword immediately returns from the body of a function. It is "
1914 "optionally followed by an expression to return. A function can also return a "
1915 "value by having its top-level block produce an expression."
1917 "`return` キーワードにより、呼び出し中の関数を即座に復帰させることができます。"
1918 "`return` の後に式を書くことで、呼び出し元へ戻り値として返すことも可能です。ま"
1919 "た、関数トップレベルのブロックを式と解釈した場合の値 (ブロック内最後の式の"
1923 #: src/doc/tutorial.md:852
1925 #| msgid "~~~~ fn line(a: int, b: int, x: int) -> int { a * x + b } fn oops(a: int, b: int, x: int) -> () { a * x + b; }"
1928 "fn line(a: int, b: int, x: int) -> int {\n"
1934 "fn line(a: int, b: int, x: int) -> int { a * x + b }\n"
1935 "fn oops(a: int, b: int, x: int) -> () { a * x + b; }"
1938 #: src/doc/tutorial.md:859
1940 "It's better Rust style to write a return value this way instead of writing "
1941 "an explicit `return`. The utility of `return` comes in when returning early "
1942 "from a function. Functions that do not return a value are said to return "
1943 "nil, `()`, and both the return type and the return value may be omitted from "
1944 "the definition. The following two functions are equivalent."
1946 "Rust では、明示的に `return` を書くのではなく、上記のような方法で戻り値を返す"
1947 "スタイルが推奨されています。 `return` は、関数を途中で復帰させたい場合に使い"
1948 "ます。値を返さない関数は、 nil `()` を返す関数として取り扱われ、関数の戻り値"
1949 "の型や、戻り値を返す処理を省略することが可能です。以下の2つの関数は等価です。"
1952 #: src/doc/tutorial.md:862
1953 msgid "~~~~ fn do_nothing_the_hard_way() -> () { return (); }"
1956 "fn do_nothing_the_hard_way() -> () { return (); }"
1959 #: src/doc/tutorial.md:865
1960 msgid "fn do_nothing_the_easy_way() { } ~~~~"
1962 "fn do_nothing_the_easy_way() { }\n"
1966 #: src/doc/tutorial.md:867
1968 "Ending the function with a semicolon like so is equivalent to returning `()`."
1969 msgstr "以下のように、セミコロンで終わる関数は `()` を返す関数と等価です。"
1972 #: src/doc/tutorial.md:871
1974 "~~~~ fn line(a: int, b: int, x: int) -> int { a * x + b } fn oops(a: int, b: "
1975 "int, x: int) -> () { a * x + b; }"
1978 "fn line(a: int, b: int, x: int) -> int { a * x + b }\n"
1979 "fn oops(a: int, b: int, x: int) -> () { a * x + b; }"
1982 #: src/doc/tutorial.md:875
1983 msgid "assert!(8 == line(5, 3, 1)); assert!(() == oops(5, 3, 1)); ~~~~"
1985 "assert!(8 == line(5, 3, 1));\n"
1986 "assert!(() == oops(5, 3, 1));\n"
1990 #: src/doc/tutorial.md:879
1992 "As with `match` expressions and `let` bindings, function arguments support "
1993 "pattern destructuring. Like `let`, argument patterns must be irrefutable, as "
1994 "in this example that unpacks the first value from a tuple and returns it."
1996 "`match` 式や `let` による変数定義のように、関数の引数もパターンによる "
1997 "destructuring をサポートしています。`let` と同様、引数のパターンは 不可反駁 "
1998 "(irrefutable) でなければなりません。以下の例では、タプルの最初の要素を取得"
2002 #: src/doc/tutorial.md:883
2003 msgid "~~~ fn first((value, _): (int, f64)) -> int { value } ~~~"
2006 "fn first((value, _): (int, f64)) -> int { value }\n"
2010 #: src/doc/tutorial.md:889
2012 "A *destructor* is a function responsible for cleaning up the resources used "
2013 "by an object when it is no longer accessible. Destructors can be defined to "
2014 "handle the release of resources like files, sockets and heap memory."
2016 "**デストラクタ** はアクセスできなくなったオブジェクトから利用されていたリソー"
2017 "スを解放する役割を持つ関数です。デストラクタはファイルやソケット、ヒープメモ"
2018 "リのようなリソースの解放を処理するために定義することができます。"
2021 #: src/doc/tutorial.md:893
2024 #| "Objects are never accessible after their destructor has been called, so "
2025 #| "there are no dynamic failures from accessing freed resources. When a task "
2026 #| "fails, the destructors of all objects in the task are called."
2028 "Objects are never accessible after their destructor has been called, so no "
2029 "dynamic failures are possible from accessing freed resources. When a task "
2030 "fails, destructors of all objects in the task are called."
2032 "オブジェクトは、デストラクタが呼び出された後にアクセス不能になります。そのた"
2033 "め、解放済みリソースへアクセスしたことによる動的なエラーは発生しません。タス"
2034 "クの実行に失敗した場合、タスクに属するすべてのオブジェクトのデストラクタが呼"
2038 #: src/doc/tutorial.md:895
2040 "The `~` sigil represents a unique handle for a memory allocation on the heap:"
2042 "シジル `~` はヒープに獲得されたメモリへのユニークな (唯一の) ハンドルを表しま"
2046 #: src/doc/tutorial.md:907
2048 "Rust includes syntax for heap memory allocation in the language since it's "
2049 "commonly used, but the same semantics can be implemented by a type with a "
2050 "custom destructor."
2052 "Rust はヒープメモリ獲得のための構文を持っています。これは、ヒープメモリ獲得が"
2053 "よく利用されるというのが理由ですが、同じ動作は独自のデストラクタを持つ型によ"
2057 #: src/doc/tutorial.md:909
2062 #: src/doc/tutorial.md:914
2064 "Rust formalizes the concept of object ownership to delegate management of an "
2065 "object's lifetime to either a variable or a task-local garbage collector. An "
2066 "object's owner is responsible for managing the lifetime of the object by "
2067 "calling the destructor, and the owner determines whether the object is "
2070 "Rust は、オブジェクトの寿命の管理を変数またはタスクローカルのガベージコレクタ"
2071 "に委譲するために、オブジェクトの所有権という考え方を取り入れています。オブ"
2072 "ジェクトの所有者はデストラクタを呼び出すことにより、オブジェクトの寿命を管理"
2073 "する責任を負っています。また、所有者はオブジェクトがミュータブルかどうかも判"
2077 #: src/doc/tutorial.md:918
2080 #| "Ownership is recursive, so mutability is inherited recursively and a "
2081 #| "destructor destroys the contained tree of owned objects. Variables are "
2082 #| "top-level owners and destroy the contained object when they go out of "
2083 #| "scope. A box managed by the garbage collector starts a new ownership "
2084 #| "tree, and the destructor is called when it is collected."
2086 "Ownership is recursive, so mutability is inherited recursively and a "
2087 "destructor destroys the contained tree of owned objects. Variables are top-"
2088 "level owners and destroy the contained object when they go out of scope."
2090 "所有権は再帰的であるため、ミュータビリティは再帰的に継承され、デストラクタは"
2091 "所有しているオブジェクトの含まれているツリーを破壊します。変数はトップレベル"
2092 "の所有者です。変数の存在しているスコープを抜けるタイミングで、変数は所有して"
2093 "いるオブジェクトを破棄します。ガベージコレクタによって管理されるボックスは、"
2094 "新しい所有権ツリーを生成し、ガベージコレクタによりオブジェクトが回収されると"
2098 #: src/doc/tutorial.md:922
2100 "~~~~ // the struct owns the objects contained in the `x` and `y` fields "
2101 "struct Foo { x: int, y: ~int }"
2104 "// この構造体はフィールド `x` と `y` に含まれるオブジェクトを所有している\n"
2105 "struct Foo { x: int, y: ~int }"
2108 #: src/doc/tutorial.md:929
2112 " // `a` is the owner of the struct, and thus the owner of the struct's fields\n"
2113 " let a = Foo { x: 5, y: ~10 };\n"
2115 "// when `a` goes out of scope, the destructor for the `~int` in the struct's\n"
2116 "// field is called\n"
2119 " // `a` は構造体の所有者であり、構造体のフィールドの所有者でもある\n"
2120 " let a = Foo { x: 5, y: ~10 };\n"
2122 "// `a` の含まれているスコープから抜けるとき、 構造体のフィールドの `~int` のデストラクタが呼ばれる\n"
2125 #: src/doc/tutorial.md:934
2127 "// `b` is mutable, and the mutability is inherited by the objects it owns "
2128 "let mut b = Foo { x: 5, y: ~10 }; b.x = 10; ~~~~"
2130 "// `b` はミュータブルなので、所有しているオブジェクトにもミュターブル性が継承"
2132 "let mut b = Foo { x: 5, y: ~10 };\n"
2137 #: src/doc/tutorial.md:941
2140 #| "If an object doesn't contain garbage-collected boxes, it consists of a "
2141 #| "single ownership tree and is given the `Owned` trait which allows it to "
2142 #| "be sent between tasks. Custom destructors can only be implemented "
2143 #| "directly on types that are `Owned`, but garbage-collected boxes can still "
2144 #| "*contain* types with custom destructors."
2146 "If an object doesn't contain any non-Send types, it consists of a single "
2147 "ownership tree and is itself given the `Send` trait which allows it to be "
2148 "sent between tasks. Custom destructors can only be implemented directly on "
2149 "types that are `Send`, but non-`Send` types can still *contain* types with "
2150 "custom destructors. Example of types which are not `Send` are [`Gc<T>`][gc] "
2151 "and [`Rc<T>`][rc], the shared-ownership types."
2153 "オブジェクトにガベージコレクトされるボックスが含まれていない場合、オブジェク"
2154 "トは単一の継承ツリーからの構成され、`Owned` トレイトが付与されます。`Owned` "
2155 "トレイトが付与されたデータは、タスク間を跨いで受け渡すことが可能です。独自の"
2156 "デストラクタは、`Owned` トレイトを満たす型に対して直接実装されなければなりま"
2157 "せんが、ガベージコレクトされるボックスが独自のデストラクタをもつ型を **含む"
2161 #: src/doc/tutorial.md:944
2164 #| "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz [win-exe]: "
2165 #| "http://static.rust-lang.org/dist/rust-nightly-install.exe"
2167 "[gc]: http://static.rust-lang.org/doc/master/std/gc/struct.Gc.html [rc]: "
2168 "http://static.rust-lang.org/doc/master/std/rc/struct.Rc.html"
2170 "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz\n"
2171 "[win-exe]: http://static.rust-lang.org/dist/rust-nightly-install.exe"
2174 #: src/doc/tutorial.md:946
2176 #| msgid "# Introduction"
2177 msgid "# Implementing a linked list"
2178 msgstr "# イントロダクション"
2181 #: src/doc/tutorial.md:973
2188 #: src/doc/tutorial.md:986
2190 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
2201 "extern crate world;\n"
2202 "fn main() { println(~\"hello \" + world::explore()); }\n"
2206 #: src/doc/tutorial.md:1047
2208 #| msgid "# Move semantics"
2209 msgid "## Move semantics"
2210 msgstr "# ムーブセマンティクス"
2213 #: src/doc/tutorial.md:1091
2216 #| "~~~~ let x = ~5; let y = x.clone(); // y is a newly allocated box let z = "
2217 #| "x; // no new memory allocated, x can no longer be used ~~~~"
2219 "~~~~ let x = ~5; let y = x.clone(); // `y` is a newly allocated box let z = "
2220 "x; // no new memory allocated, `x` can no longer be used ~~~~"
2224 "let y = x.clone();\n"
2225 "// y は新しく獲得されるボックス\n"
2226 "let z = x; // 新たなメモリの獲得は行われない。x を使うことはできなくなる\n"
2230 #: src/doc/tutorial.md:1120
2232 "~~~~ let r = ~13; let mut s = r; // box becomes mutable *s += 1; let t = "
2233 "s; // box becomes immutable ~~~~"
2237 "let mut s = r; // ボックスはミュータブルになる\n"
2239 "let t = s; // ボックスはイミュータブルになる\n"
2243 #: src/doc/tutorial.md:1144
2245 msgid "## References"
2246 msgstr "# ポインタのデリファレンス"
2249 #: src/doc/tutorial.md:1190
2251 #| msgid "## Using other crates"
2252 msgid "## Lists of other types"
2253 msgstr "## 他のクレートの利用"
2256 #: src/doc/tutorial.md:1322
2258 #| msgid "## Managed boxes"
2259 msgid "# More on boxes"
2260 msgstr "## マネージドボックス"
2263 #: src/doc/tutorial.md:1353
2264 msgid "~~~~ let x = 5; // immutable let mut y = 5; // mutable y += 2;"
2267 "let x = 5; // イミュータブル\n"
2268 "let mut y = 5; // ミュータブル\n"
2272 #: src/doc/tutorial.md:1358
2275 #| "let x = ~5; // immutable let mut y = ~5; // mutable *y += 2; // the * "
2276 #| "operator is needed to access the contained value ~~~~"
2278 "let x = ~5; // immutable let mut y = ~5; // mutable *y += 2; // the `*` "
2279 "operator is needed to access the contained value ~~~~"
2281 "let x = ~5; // イミュータブル\n"
2282 "let mut y = ~5; // ミュータブル\n"
2283 "*y += 2; // ボックスの中身にアクセスするには、 * 演算子が必要\n"
2287 #: src/doc/tutorial.md:1367
2290 #| "Rust's borrowed pointers are a general purpose reference type. In "
2291 #| "contrast with owned boxes, where the holder of an owned box is the owner "
2292 #| "of the pointed-to memory, borrowed pointers never imply ownership. A "
2293 #| "pointer can be borrowed to any object, and the compiler verifies that it "
2294 #| "cannot outlive the lifetime of the object."
2296 "In contrast with owned boxes, where the holder of an owned box is the owner "
2297 "of the pointed-to memory, references never imply ownership - they are "
2298 "\"borrowed\". A reference can be borrowed to any object, and the compiler "
2299 "verifies that it cannot outlive the lifetime of the object."
2301 "Rust の借用ポインタ (borrowed pointer) は汎用的な参照型です。所有ボックスの場"
2302 "合、ボックスの所有者が参照されているメモリの所有者となるのに対して、借用ポイ"
2303 "ンタを所有することがメモリを所有を意味することはありません。ポインタは任意の"
2304 "オブジェクトから借用することが可能で、参照先のオブジェクトよりもポインタが長"
2305 "生きしないことがコンパイラにより保証されます。"
2308 #: src/doc/tutorial.md:1369
2309 msgid "As an example, consider a simple struct type, `Point`:"
2310 msgstr "例として、シンプルな構造体型の `Point` について考えます。"
2313 #: src/doc/tutorial.md:1376
2315 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
2326 "extern crate world;\n"
2327 "fn main() { println(~\"hello \" + world::explore()); }\n"
2331 #: src/doc/tutorial.md:1380
2333 "We can use this simple definition to allocate points in many different ways. "
2334 "For example, in this code, each of these three local variables contains a "
2335 "point, but allocated in a different location:"
2337 "シンプルな定義ですが、この定義を使って `Point` 型のオブジェクトを様々な方法で"
2338 "割り当てることができます。例えば、このコードの3つのローカル変数は、それぞれ異"
2339 "なった場所に `Point` 型のオブジェクトを割り当てています。"
2342 #: src/doc/tutorial.md:1387
2344 #| msgid "~~~~ {.ignore} # struct Point { x: f64, y: f64 } let mut mypoint = Point { x: 1.0, y: 1.0 }; let origin = Point { x: 0.0, y: 0.0 };"
2347 "# struct Point { x: f64, y: f64 }\n"
2348 "let on_the_stack : Point = Point { x: 3.0, y: 4.0 };\n"
2349 "let managed_box : @Point = @Point { x: 5.0, y: 1.0 };\n"
2350 "let owned_box : ~Point = ~Point { x: 7.0, y: 9.0 };\n"
2354 "# struct Point { x: f64, y: f64 }\n"
2355 "let mut mypoint = Point { x: 1.0, y: 1.0 };\n"
2356 "let origin = Point { x: 0.0, y: 0.0 };"
2359 #: src/doc/tutorial.md:1397
2362 #| "Suppose we want to write a procedure that computes the distance between "
2363 #| "any two points, no matter where they are stored. For example, we might "
2364 #| "like to compute the distance between `on_the_stack` and `managed_box`, or "
2365 #| "between `managed_box` and `owned_box`. One option is to define a function "
2366 #| "that takes two arguments of type point—that is, it takes the points by "
2367 #| "value. But this will cause the points to be copied when we call the "
2368 #| "function. For points, this is probably not so bad, but often copies are "
2369 #| "expensive. So we’d like to define a function that takes the points by "
2370 #| "pointer. We can use borrowed pointers to do this:"
2372 "Suppose we want to write a procedure that computes the distance between any "
2373 "two points, no matter where they are stored. For example, we might like to "
2374 "compute the distance between `on_the_stack` and `managed_box`, or between "
2375 "`managed_box` and `owned_box`. One option is to define a function that takes "
2376 "two arguments of type point—that is, it takes the points by value. But this "
2377 "will cause the points to be copied when we call the function. For points, "
2378 "this is probably not so bad, but often copies are expensive. So we’d like to "
2379 "define a function that takes the points by pointer. We can use references to "
2382 "`Point` 型のオブジェクトの割り当て先がどこであったとしても利用可能な、任意の "
2383 "2 点間の距離を計算する処理を書きたいとします。例えば、 `on_the_stack`, "
2384 "`managed_box` 間や `managed_box`, `owned_box` 間の距離を計算する処理です。 1"
2385 "つ目の実装方法として、2つの `Point` 型オブジェクトを引数にとる関数を定義する"
2386 "方法、すなわち、オブジェクトを値で受け渡す方法があります。しかし、この方法で"
2387 "は関数呼び出し時に `Point` オブジェクトのコピーが行われます。`Point` オブジェ"
2388 "クトの場合、このような実装はそれほど悪いものではないでしょうが、コピー処理の"
2389 "コストは高い場合もあります。したがって、`Point` オブジェクトをポインタ渡しす"
2390 "る関数を定義する必要があります。そのために、借用ポインタを利用することが可能"
2394 #: src/doc/tutorial.md:1419
2396 #| msgid "~~~~ {.ignore} # struct Point { x: f64, y: f64 } let mut mypoint = Point { x: 1.0, y: 1.0 }; let origin = Point { x: 0.0, y: 0.0 };"
2399 "# struct Point{ x: f64, y: f64 };\n"
2400 "# let on_the_stack : Point = Point { x: 3.0, y: 4.0 };\n"
2401 "# let managed_box : @Point = @Point { x: 5.0, y: 1.0 };\n"
2402 "# let owned_box : ~Point = ~Point { x: 7.0, y: 9.0 };\n"
2403 "# fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }\n"
2404 "compute_distance(&on_the_stack, managed_box);\n"
2405 "compute_distance(managed_box, owned_box);\n"
2409 "# struct Point { x: f64, y: f64 }\n"
2410 "let mut mypoint = Point { x: 1.0, y: 1.0 };\n"
2411 "let origin = Point { x: 0.0, y: 0.0 };"
2414 #: src/doc/tutorial.md:1426
2417 #| "Here the `&` operator is used to take the address of the variable "
2418 #| "`on_the_stack`; this is because `on_the_stack` has the type `Point` (that "
2419 #| "is, a struct value) and we have to take its address to get a value. We "
2420 #| "also call this _borrowing_ the local variable `on_the_stack`, because we "
2421 #| "are creating an alias: that is, another route to the same data."
2423 "Here the `&` operator is used to take the address of the variable "
2424 "`on_the_stack`; this is because `on_the_stack` has the type `Point` (that "
2425 "is, a struct value) and we have to take its address to get a reference. We "
2426 "also call this _borrowing_ the local variable `on_the_stack`, because we are "
2427 "creating an alias: that is, another route to the same data."
2429 "ここで `&` 演算子は `on_the_stack` 変数のアドレスを取得するために使われていま"
2430 "す。これは、 `on_the_stack` の型は `Point` (つまり、構造体の値) であり、呼び"
2431 "出した関数から値を取得させるため、構造体のアドレスを渡す必要があるからです。"
2432 "値の別名 (エイリアス)、すなわち、同じデータへアクセスするための別の方法を提供"
2433 "するので、このような操作のことをローカル変数 `on_the_stack` の __借用__ "
2434 "(_borrowing_) と呼びます。"
2437 #: src/doc/tutorial.md:1432
2440 #| "In the case of the boxes `managed_box` and `owned_box`, however, no "
2441 #| "explicit action is necessary. The compiler will automatically convert a "
2442 #| "box like `@point` or `~point` to a borrowed pointer like `&point`. This "
2443 #| "is another form of borrowing; in this case, the contents of the managed/"
2444 #| "owned box are being lent out."
2446 "In the case of the boxes `managed_box` and `owned_box`, however, no explicit "
2447 "action is necessary. The compiler will automatically convert a box like "
2448 "`@point` or `~point` to a reference like `&point`. This is another form of "
2449 "borrowing; in this case, the contents of the managed/owned box are being "
2452 "ボックスである `managed_box` と `owned_box` の場合は、特に明示的な操作を行う"
2453 "必要はありません。コンパイラは `@point` や `~point` のようなボックスを自動的"
2454 "に `&point` のような借用ポインタへと変換します。これは、別の形態の借用 "
2455 "(borrowing) です。この場合、マネージド/所有ボックスの内容が貸し出されていま"
2459 #: src/doc/tutorial.md:1441
2462 #| "Whenever a value is borrowed, there are some limitations on what you can "
2463 #| "do with the original. For example, if the contents of a variable have "
2464 #| "been lent out, you cannot send that variable to another task, nor will "
2465 #| "you be permitted to take actions that might cause the borrowed value to "
2466 #| "be freed or to change its type. This rule should make intuitive sense: "
2467 #| "you must wait for a borrowed value to be returned (that is, for the "
2468 #| "borrowed pointer to go out of scope) before you can make full use of it "
2471 "Whenever a value is borrowed, there are some limitations on what you can do "
2472 "with the original. For example, if the contents of a variable have been lent "
2473 "out, you cannot send that variable to another task, nor will you be "
2474 "permitted to take actions that might cause the borrowed value to be freed or "
2475 "to change its type. This rule should make intuitive sense: you must wait for "
2476 "a borrowed value to be returned (that is, for the reference to go out of "
2477 "scope) before you can make full use of it again."
2479 "値が借用されている間、借用元の値に対して行える操作がいくらか制限されます。例"
2480 "えば、変数の内容が貸し出された場合、その変数を他のタスクに送信することはでき"
2481 "ませんし、借用された値を解放したり、型が変化させるような操作も行うことができ"
2482 "ません。このルールは理にかなったものでしょう。貸し出した値を最大限に活用する "
2483 "(make full use of it) ためには、貸し出した値の返却 (借用ポインタが存在するス"
2484 "コープを抜ける) を待たなければなりません。"
2487 #: src/doc/tutorial.md:1444
2490 #| "For a more in-depth explanation of borrowed pointers, read the [borrowed "
2491 #| "pointer tutorial][borrowtut]."
2493 "For a more in-depth explanation of references and lifetimes, read the "
2494 "[references and lifetimes guide][lifetimes]."
2496 "借用ポインタの詳細については、[借用ポインタのチュートリアル][borrowtut]を参照"
2500 #: src/doc/tutorial.md:1446
2505 #: src/doc/tutorial.md:1450
2508 #| "Borrowing an immutable pointer to an object freezes it and prevents "
2509 #| "mutation. `Owned` objects have freezing enforced statically at compile-"
2512 "Lending an immutable pointer to an object freezes it and prevents mutation. "
2513 "`Freeze` objects have freezing enforced statically at compile-time. An "
2514 "example of a non-`Freeze` type is [`RefCell<T>`][refcell]."
2516 "オブジェクトへのイミュータブルな (借用) ポインタを借用した場合、借用されたオ"
2517 "ブジェクトは凍結 (freezing) され、変更することができなくなります。`Owned` ト"
2518 "レイトが付与されたオブジェクトは、コンパイル時の静的解析により、強制的に凍結"
2519 "されます (凍結された値を変更しようとすると、コンパイルエラーとなります)。"
2522 #: src/doc/tutorial.md:1459
2526 #| "let mut x = 5;\n"
2528 #| " let y = &x; // x is now frozen, it cannot be modified\n"
2530 #| "// x is now unfrozen again\n"
2537 " let y = &x; // `x` is now frozen, it cannot be modified\n"
2539 "// `x` is now unfrozen again\n"
2546 " let y = &x; // x は凍結されたので、変更することができない\n"
2548 "// x の凍結状態は解除される\n"
2553 #: src/doc/tutorial.md:1461
2556 #| "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz [win-exe]: "
2557 #| "http://static.rust-lang.org/dist/rust-nightly-install.exe"
2559 "[refcell]: http://static.rust-lang.org/doc/master/std/cell/struct.RefCell."
2562 "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz\n"
2563 "[win-exe]: http://static.rust-lang.org/dist/rust-nightly-install.exe"
2566 #: src/doc/tutorial.md:1463
2567 msgid "# Dereferencing pointers"
2568 msgstr "# ポインタのデリファレンス"
2571 #: src/doc/tutorial.md:1466
2573 "Rust uses the unary star operator (`*`) to access the contents of a box or "
2574 "pointer, similarly to C."
2576 "Rust では、C と同様、ボックスの内容やポインタの参照先にアクセスするためには単"
2577 "項スター演算子 (`*`) を使います。"
2580 #: src/doc/tutorial.md:1471
2581 msgid "~~~ let managed = @10; let owned = ~20; let borrowed = &30;"
2584 "let managed = @10;\n"
2585 "let owned = ~20;\n"
2586 "let borrowed = &30;"
2589 #: src/doc/tutorial.md:1474
2590 msgid "let sum = *managed + *owned + *borrowed; ~~~"
2592 "let sum = *managed + *owned + *borrowed;\n"
2596 #: src/doc/tutorial.md:1478
2598 "Dereferenced mutable pointers may appear on the left hand side of "
2599 "assignments. Such an assignment modifies the value that the pointer points "
2602 "ミュータブルなポインタをデリファレンスしたものは、代入文の左辺に置くことがで"
2603 "きます。このような代入文は、ポインタが指す値を変更します。"
2606 #: src/doc/tutorial.md:1482
2608 #| msgid "~~~ let managed = @mut 10; let mut owned = ~20;"
2609 msgid "~~~ let managed = @10; let mut owned = ~20;"
2612 "let managed = @mut 10;\n"
2613 "let mut owned = ~20;"
2616 #: src/doc/tutorial.md:1485
2617 msgid "let mut value = 30; let borrowed = &mut value;"
2619 "let mut value = 30;\n"
2620 "let borrowed = &mut value;"
2623 #: src/doc/tutorial.md:1493
2625 "Pointers have high operator precedence, but lower precedence than the dot "
2626 "operator used for field and method access. This precedence order can "
2627 "sometimes make code awkward and parenthesis-filled."
2629 "ポインタ演算子の優先順位は高いですが、フィールドやメソッドのアクセスに用いる"
2630 "ドット演算子よりは優先順位は低いです。この優先順位により、コードが不格好で括"
2631 "弧だらけなものになることがあります。"
2634 #: src/doc/tutorial.md:1503
2637 #| "~~~ # struct Point { x: f64, y: f64 } # enum Shape { Rectangle(Point, "
2638 #| "Point) } # impl Shape { fn area(&self) -> int { 0 } } let start = @Point "
2639 #| "{ x: 10f, y: 20f }; let end = ~Point { x: (*start).x + 100f, y: (*start)."
2640 #| "y + 100f }; let rect = &Rectangle(*start, *end); let area = (*rect)."
2643 "~~~ # struct Point { x: f64, y: f64 } # enum Shape { Rectangle(Point, "
2644 "Point) } # impl Shape { fn area(&self) -> int { 0 } } let start = @Point "
2645 "{ x: 10.0, y: 20.0 }; let end = ~Point { x: (*start).x + 100.0, y: (*start)."
2646 "y + 100.0 }; let rect = &Rectangle(*start, *end); let area = (*rect).area(); "
2650 "# struct Point { x: f64, y: f64 }\n"
2651 "# enum Shape { Rectangle(Point, Point) }\n"
2652 "# impl Shape { fn area(&self) -> int { 0 } }\n"
2653 "let start = @Point { x: 10f, y: 20f };\n"
2654 "let end = ~Point { x: (*start).x + 100f, y: (*start).y + 100f };\n"
2655 "let rect = &Rectangle(*start, *end);\n"
2656 "let area = (*rect).area();\n"
2660 #: src/doc/tutorial.md:1507
2662 "To combat this ugliness the dot operator applies _automatic pointer "
2663 "dereferencing_ to the receiver (the value on the left-hand side of the dot), "
2664 "so in most cases, explicitly dereferencing the receiver is not necessary."
2666 "コードが醜くなるのを防ぐため、ドット演算子はレシーバ (ドットの左にある値) の "
2667 "__ポインタを自動的にデリファレンス__ します。これにより、ほとんどのケースでは"
2668 "レシーバを明示的にデリファレンスする必要がなくなります。"
2671 #: src/doc/tutorial.md:1517
2674 #| "~~~ # struct Point { x: f64, y: f64 } # enum Shape { Rectangle(Point, "
2675 #| "Point) } # impl Shape { fn area(&self) -> int { 0 } } let start = @Point "
2676 #| "{ x: 10f, y: 20f }; let end = ~Point { x: start.x + 100f, y: start.y + "
2677 #| "100f }; let rect = &Rectangle(*start, *end); let area = rect.area(); ~~~"
2679 "~~~ # struct Point { x: f64, y: f64 } # enum Shape { Rectangle(Point, "
2680 "Point) } # impl Shape { fn area(&self) -> int { 0 } } let start = @Point "
2681 "{ x: 10.0, y: 20.0 }; let end = ~Point { x: start.x + 100.0, y: start.y + "
2682 "100.0 }; let rect = &Rectangle(*start, *end); let area = rect.area(); ~~~"
2685 "# struct Point { x: f64, y: f64 }\n"
2686 "# enum Shape { Rectangle(Point, Point) }\n"
2687 "# impl Shape { fn area(&self) -> int { 0 } }\n"
2688 "let start = @Point { x: 10f, y: 20f };\n"
2689 "let end = ~Point { x: start.x + 100f, y: start.y + 100f };\n"
2690 "let rect = &Rectangle(*start, *end);\n"
2691 "let area = rect.area();\n"
2695 #: src/doc/tutorial.md:1521
2697 "You can write an expression that dereferences any number of pointers "
2698 "automatically. For example, if you feel inclined, you could write something "
2701 "1つのドット演算子で何度も自動デリファレンスが行うことができます。例えば、やろ"
2702 "うと思えば以下のような馬鹿げたものを書くこともできます。"
2705 #: src/doc/tutorial.md:1527
2708 #| "~~~ # struct Point { x: f64, y: f64 } let point = &@~Point { x: 10f, y: "
2709 #| "20f }; println(fmt!(\"%f\", point.x)); ~~~"
2711 "~~~ # struct Point { x: f64, y: f64 } let point = &@~Point { x: 10.0, y: "
2712 "20.0 }; println!(\"{:f}\", point.x); ~~~"
2715 "# struct Point { x: f64, y: f64 }\n"
2716 "let point = &@~Point { x: 10f, y: 20f };\n"
2717 "println(fmt!(\"%f\", point.x));\n"
2721 #: src/doc/tutorial.md:1529
2722 msgid "The indexing operator (`[]`) also auto-dereferences."
2723 msgstr "添字演算子 (`[]`) も自動でリファレンスを行います。"
2726 #: src/doc/tutorial.md:1531
2727 msgid "# Vectors and strings"
2731 #: src/doc/tutorial.md:1535
2734 #| "A vector is a contiguous section of memory containing zero or more values "
2735 #| "of the same type. Like other types in Rust, vectors can be stored on the "
2736 #| "stack, the local heap, or the exchange heap. Borrowed pointers to vectors "
2737 #| "are also called 'slices'."
2739 "A vector is a contiguous block of memory containing zero or more values of "
2740 "the same type. Rust also supports vector reference types, called slices, "
2741 "which are a view into a block of memory represented as a pointer and a "
2744 "ベクタは同じ型の値が0個以上含まれる、メモリ上の連続した部分のことです。Rust "
2745 "の他の型と同様、ベクタもスタック、ローカルヒープ、交換ヒープ (exchange heap) "
2746 "上に格納することができます。ベクトルの借用ポインタは、「スライス」と呼ばれる"
2750 #: src/doc/tutorial.md:1608
2751 msgid "Square brackets denote indexing into a vector:"
2752 msgstr "角括弧はベクタの添字を表します。"
2755 #: src/doc/tutorial.md:1622
2756 msgid "A vector can be destructured using pattern matching:"
2757 msgstr "ベクタはパターンマッチにより destructuring することができます。"
2760 #: src/doc/tutorial.md:1635
2763 #| "Both vectors and strings support a number of useful [methods](#methods), "
2764 #| "defined in [`std::vec`] and [`std::str`]. Here are some examples."
2766 "Both vectors and strings support a number of useful [methods](#methods), "
2767 "defined in [`std::vec`] and [`std::str`]."
2769 "ベクタと文字列は、[`std::vec`] と [`std::str`] で定義された、多くの有用な [メ"
2770 "ソッド](#methods) を持ちます。以下にいくつか例を挙げます。"
2773 #: src/doc/tutorial.md:1638
2775 #| msgid "[`std::vec`]: std/vec.html [`std::str`]: std/str.html"
2776 msgid "[`std::vec`]: std/vec/index.html [`std::str`]: std/str/index.html"
2778 "[`std::vec`]: std/vec.html\n"
2779 "[`std::str`]: std/str.html"
2782 #: src/doc/tutorial.md:1650
2784 #| msgid "~~~~ use std::task::spawn;"
2785 msgid "~~~ use std::rc::Rc;"
2788 "use std::task::spawn;"
2791 #: src/doc/tutorial.md:1669
2793 #| msgid "~~~~ use std::task::spawn;"
2794 msgid "~~~ use std::gc::Gc;"
2797 "use std::task::spawn;"
2800 #: src/doc/tutorial.md:1686
2805 #: src/doc/tutorial.md:1691
2807 "Named functions, like those we've seen so far, may not refer to local "
2808 "variables declared outside the function: they do not close over their "
2809 "environment (sometimes referred to as \"capturing\" variables in their "
2810 "environment). For example, you couldn't write the following:"
2812 "これまで登場したような名前のある関数は、関数の外で定義されるローカル変数を参"
2813 "照することはできません。ローカル変数は環境を閉じ込める (環境中の変数を「キャ"
2814 "プチャする」と呼ばれることもあります) ことはありません。例えば、以下のような"
2818 #: src/doc/tutorial.md:1694
2819 msgid "~~~~ {.ignore} let foo = 10;"
2825 #: src/doc/tutorial.md:1699
2828 "fn bar() -> int {\n"
2829 " return foo; // `bar` cannot refer to `foo`\n"
2833 "fn bar() -> int {\n"
2834 " return foo; // `bar` ば `foo` を参照できない\n"
2839 #: src/doc/tutorial.md:1702
2841 "Rust also supports _closures_, functions that can access variables in the "
2844 "Rust は __クロージャ__ という、周囲のスコープの変数にアクセスできる関数をサ"
2848 #: src/doc/tutorial.md:1705
2849 msgid "~~~~ fn call_closure_with_ten(b: |int|) { b(10); }"
2852 "fn call_closure_with_ten(b: |int|) { b(10); }"
2855 #: src/doc/tutorial.md:1708
2858 #| "let captured_var = 20; let closure = |arg| println(fmt!(\"captured_var="
2859 #| "%d, arg=%d\", captured_var, arg));"
2861 "let captured_var = 20; let closure = |arg| println!(\"captured_var={}, "
2862 "arg={}\", captured_var, arg);"
2864 "let captured_var = 20;\n"
2865 "let closure = |arg| println(fmt!(\"captured_var=%d, arg=%d\", captured_var, "
2869 #: src/doc/tutorial.md:1711
2870 msgid "call_closure_with_ten(closure); ~~~~"
2872 "call_closure_with_ten(closure);\n"
2876 #: src/doc/tutorial.md:1717
2878 "Closures begin with the argument list between vertical bars and are followed "
2879 "by a single expression. Remember that a block, `{ <expr1>; <expr2>; ... }`, "
2880 "is considered a single expression: it evaluates to the result of the last "
2881 "expression it contains if that expression is not followed by a semicolon, "
2882 "otherwise the block evaluates to `()`."
2884 "クロージャはバーティカルバー (`|`) で囲まれた引数リストと、それに続く単一の式"
2885 "から構成されます。ブロック `{ <expr1>; <expr2>; ...}` は単一の式とみなされる"
2886 "ことを思い出してください。ブロックに含まれる最後の式に続けてセミコロンがない"
2887 "場合、ブロックの値は最後の式の値となり、そうでなければ `()` となります。"
2890 #: src/doc/tutorial.md:1722
2892 "The types of the arguments are generally omitted, as is the return type, "
2893 "because the compiler can almost always infer them. In the rare case where "
2894 "the compiler needs assistance, though, the arguments and return types may be "
2897 "引数の型や戻り値の型は、ほとんどすべての場合においてコンパイラにより推論され"
2898 "るため、通常省略できます。発生するのはまれですが、コンパイラが推論に失敗する"
2899 "場合は、引数と戻り値の型注釈を付けることがあります。"
2902 #: src/doc/tutorial.md:1726
2903 msgid "~~~~ let square = |x: int| -> uint { (x * x) as uint }; ~~~~"
2906 "let square = |x: int| -> uint { (x * x) as uint };\n"
2910 #: src/doc/tutorial.md:1730
2912 "There are several forms of closure, each with its own role. The most common, "
2913 "called a _stack closure_, has type `||` and can directly access local "
2914 "variables in the enclosing scope."
2916 "クロージャにはいくつかの形態があり、それぞれに独自の役割があります。最も一般"
2917 "的なのはスタッククロージャと呼ばれるもので、 `||` という型を持ち、外側のロー"
2918 "カル変数に直接アクセスすることができます。"
2921 #: src/doc/tutorial.md:1735
2922 msgid "~~~~ let mut max = 0; [1, 2, 3].map(|x| if *x > max { max = *x }); ~~~~"
2925 "let mut max = 0;\n"
2926 "[1, 2, 3].map(|x| if *x > max { max = *x });\n"
2930 #: src/doc/tutorial.md:1744
2932 "Stack closures are very efficient because their environment is allocated on "
2933 "the call stack and refers by pointer to captured locals. To ensure that "
2934 "stack closures never outlive the local variables to which they refer, stack "
2935 "closures are not first-class. That is, they can only be used in argument "
2936 "position; they cannot be stored in data structures or returned from "
2937 "functions. Despite these limitations, stack closures are used pervasively in "
2940 "スタッククロージャは、閉じ込める環境はコールスタック上に獲得され、ローカル変"
2941 "数をポインタで参照するため、非常に効率的です。スタッククロージャが参照してい"
2942 "るローカル変数よりも長生きしないことを保証するため、スタッククロージャは第一"
2943 "級の値ではありません。すなわち、スタッククロージャは引数としてしか使うことが"
2944 "できず、データ構造や関数の戻り値となることはありません。この制限にも関わら"
2945 "ず、スタッククロージャは Rust のコードのあちこちに登場します。"
2948 #: src/doc/tutorial.md:1746
2949 msgid "## Owned closures"
2953 #: src/doc/tutorial.md:1753
2955 "Owned closures, written `proc`, hold on to things that can safely be sent "
2956 "between processes. They copy the values they close over, much like managed "
2957 "closures, but they also own them: that is, no other code can access them. "
2958 "Owned closures are used in concurrent code, particularly for spawning [tasks]"
2961 "`~` `proc` で書き表される所有クロージャは安全にプロセス間で送信することができ"
2962 "ます。所有クローじゃはマネージドクロージャと全く同じように閉じ込める値をコ"
2963 "ピーしますが、値を所有します。つまり、他のコードは閉じ込められた値にアクセス"
2964 "できなくなります。所有クロージャは並列プログラム、特に [タスク][tasks] 生成で"
2968 #: src/doc/tutorial.md:1755
2969 msgid "## Closure compatibility"
2970 msgstr "## クロージャの互換性"
2973 #: src/doc/tutorial.md:1762
2975 "Rust closures have a convenient subtyping property: you can pass any kind of "
2976 "closure (as long as the arguments and return types match) to functions that "
2977 "expect a `||`. Thus, when writing a higher-order function that only calls "
2978 "its function argument, and does nothing else with it, you should almost "
2979 "always declare the type of that argument as `||`. That way, callers may pass "
2980 "any kind of closure."
2982 "Rust のクロージャは型の派生 (subtyping) という便利な性質を持っています。この"
2983 "性質により、`||` 型を期待する関数には (引数と戻り値の型が一致する限り) 任意の"
2984 "種類のクロージャを渡すことができます。したがって、引数で渡された関数について"
2985 "は呼び出すだけで他に何もしない高階関数を書くときには、ほぼすべてのケースで引"
2986 "数の型を `||` と宣言するべきです。そうすることで、呼び出し元は任意の種類のク"
2987 "ロージャを渡すことができるよになります。"
2990 #: src/doc/tutorial.md:1770
2992 "~~~~ fn call_twice(f: ||) { f(); f(); } let closure = || { \"I'm a closure, "
2993 "and it doesn't matter what type I am\"; }; fn function() { \"I'm a normal "
2994 "function\"; } call_twice(closure); call_twice(function); ~~~~"
2997 "fn call_twice(f: ||) { f(); f(); }\n"
2998 "let closure = || { \"I'm a closure, and it doesn't matter what type I am"
3000 "fn function() { \"I'm a normal function\"; }\n"
3001 "call_twice(closure);\n"
3002 "call_twice(function);\n"
3006 #: src/doc/tutorial.md:1774
3008 "> *Note:* Both the syntax and the semantics will be changing > in small "
3009 "ways. At the moment they can be unsound in some > scenarios, particularly "
3010 "with non-copyable types."
3012 "> *注意* コードの文法と意味は将来的に変更されるかもしれません。現時点では"
3013 "いくつかの状況、特にコピーできない型が関連するケースにおいて望ましくない振る"
3017 #: src/doc/tutorial.md:1776
3018 msgid "## Do syntax"
3022 #: src/doc/tutorial.md:1779
3025 #| "The `do` expression provides a way to treat higher-order functions "
3026 #| "(functions that take closures as arguments) as control structures."
3028 "The `do` expression makes it easier to call functions that take procedures "
3031 "`do` 式は高階関数 (クロージャを引数にとる関数) を制御構造のように取り扱う方法"
3035 #: src/doc/tutorial.md:1791
3037 "As a caller, if we use a closure to provide the final operator argument, we "
3038 "can write it in a way that has a pleasant, block-like structure."
3040 "最後の引数にクロージャをとる関数を呼び出す場合、ブロック構造を持つかのように"
3044 #: src/doc/tutorial.md:1813
3046 "If you want to see the output of `debug!` statements, you will need to turn "
3047 "on `debug!` logging. To enable `debug!` logging, set the RUST_LOG "
3048 "environment variable to the name of your crate, which, for a file named `foo."
3049 "rs`, will be `foo` (e.g., with bash, `export RUST_LOG=foo`)."
3051 "`debug!` ステートメントの出力を見たい場合、`debug!` によるロギングを有効にす"
3052 "る必要があるでしょう。`debug!` によるロギングを有効にするためには、 RUST_LOG "
3053 "環境変数をクレートの名前に設定する必要があります (例えば、bash の場合、 "
3054 "`export RUST_LOG=foo` を実行する)。 `foo.rs` というファイルの場合、クレート名"
3058 #: src/doc/tutorial.md:1815
3063 #: src/doc/tutorial.md:1821
3065 "Methods are like functions except that they always begin with a special "
3066 "argument, called `self`, which has the type of the method's receiver. The "
3067 "`self` argument is like `this` in C++ and many other languages. Methods are "
3068 "called with dot notation, as in `my_vec.len()`."
3070 "メソッドは、`self` という、メソッドのレシーバと同じ型の特別な引数を第一引数と"
3071 "してとる関数のようなものです。`self` は、 C++ や他の言語の `this` のようなも"
3072 "のです。メソッドはドット記法を浸かって `my_vec.len()` のように呼び出します。"
3075 #: src/doc/tutorial.md:1825
3077 "_Implementations_, written with the `impl` keyword, can define methods on "
3078 "most Rust types, including structs and enums. As an example, let's define a "
3079 "`draw` method on our `Shape` enum."
3081 "`impl` キーワードを使って記述される __実装__ (_implementation_) により、構造"
3082 "体や列挙型を含むほとんどの Rust の型に対してメソッドを定義することができま"
3083 "す。例のように、 `draw` メソッドを `Shape` 列挙型に定義してみましょう。"
3086 #: src/doc/tutorial.md:1833
3088 #| msgid "~~~~ {.ignore} # struct Point { x: f64, y: f64 } let mut mypoint = Point { x: 1.0, y: 1.0 }; let origin = Point { x: 0.0, y: 0.0 };"
3091 "# fn draw_circle(p: Point, f: f64) { }\n"
3092 "# fn draw_rectangle(p: Point, p: Point) { }\n"
3099 "# struct Point { x: f64, y: f64 }\n"
3100 "let mut mypoint = Point { x: 1.0, y: 1.0 };\n"
3101 "let origin = Point { x: 0.0, y: 0.0 };"
3104 #: src/doc/tutorial.md:1851
3106 #| msgid "let s = Circle(Point { x: 1f, y: 2f }, 3f); s.draw(); ~~~"
3107 msgid "let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0); s.draw(); ~~~"
3109 "let s = Circle(Point { x: 1f, y: 2f }, 3f);\n"
3114 #: src/doc/tutorial.md:1855
3116 "This defines an _implementation_ for `Shape` containing a single method, "
3117 "`draw`. In most respects the `draw` method is defined like any other "
3118 "function, except for the name `self`."
3120 "この例では、 `Shape` に1つのメソッド `draw` をもつ __実装__ を定義していま"
3121 "す。`draw` メソッドは、`self` という名前を除くほとんどの面で他の関数と同じよ"
3125 #: src/doc/tutorial.md:1860
3127 "The type of `self` is the type on which the method is implemented, or a "
3128 "pointer thereof. As an argument it is written either `self`, `&self`, "
3129 "`@self`, or `~self`. A caller must in turn have a compatible pointer type "
3130 "to call the method."
3132 "`self` の型は、メソッドが実装されている型か、それらのポインタである。引数とし"
3133 "ては、 `self`, `&self`, `@self` または `~self` と記述されます。呼び出し側も同"
3134 "様、メソッドを呼び出すための互換性のあるポインタ型をもつ必要があります。"
3137 #: src/doc/tutorial.md:1875
3141 #| "# fn draw_circle(p: Point, f: f64) { }\n"
3142 #| "# fn draw_rectangle(p: Point, p: Point) { }\n"
3143 #| "# struct Point { x: f64, y: f64 }\n"
3144 #| "# enum Shape {\n"
3145 #| "# Circle(Point, f64),\n"
3146 #| "# Rectangle(Point, Point)\n"
3148 #| "# impl Shape {\n"
3149 #| "# fn draw_borrowed(&self) { ... }\n"
3150 #| "# fn draw_managed(@self) { ... }\n"
3151 #| "# fn draw_owned(~self) { ... }\n"
3152 #| "# fn draw_value(self) { ... }\n"
3154 #| "# let s = Circle(Point { x: 1f, y: 2f }, 3f);\n"
3155 #| "// As with typical function arguments, managed and owned pointers\n"
3156 #| "// are automatically converted to borrowed pointers\n"
3159 "# fn draw_circle(p: Point, f: f64) { }\n"
3160 "# fn draw_rectangle(p: Point, p: Point) { }\n"
3161 "# struct Point { x: f64, y: f64 }\n"
3163 "# Circle(Point, f64),\n"
3164 "# Rectangle(Point, Point)\n"
3167 " fn draw_reference(&self) { ... }\n"
3168 " fn draw_managed(@self) { ... }\n"
3169 " fn draw_owned(~self) { ... }\n"
3170 " fn draw_value(self) { ... }\n"
3174 "# fn draw_circle(p: Point, f: f64) { }\n"
3175 "# fn draw_rectangle(p: Point, p: Point) { }\n"
3176 "# struct Point { x: f64, y: f64 }\n"
3178 "# Circle(Point, f64),\n"
3179 "# Rectangle(Point, Point)\n"
3182 "# fn draw_borrowed(&self) { ... }\n"
3183 "# fn draw_managed(@self) { ... }\n"
3184 "# fn draw_owned(~self) { ... }\n"
3185 "# fn draw_value(self) { ... }\n"
3187 "# let s = Circle(Point { x: 1f, y: 2f }, 3f);\n"
3188 "// 関数の引数と同様、マネージドポインタと所有ポインタは、\n"
3189 "// 自動的に借用ポインタに変換される\n"
3192 #: src/doc/tutorial.md:1877
3194 #| msgid "let s = Circle(Point { x: 1f, y: 2f }, 3f); s.draw(); ~~~"
3195 msgid "let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);"
3197 "let s = Circle(Point { x: 1f, y: 2f }, 3f);\n"
3202 #: src/doc/tutorial.md:1883
3205 #| "(@s).draw_managed(); (~s).draw_owned(); (&s).draw_borrowed(); s."
3206 #| "draw_value(); ~~~"
3208 "(@s).draw_managed(); (~s).draw_owned(); (&s).draw_reference(); s."
3211 "(@s).draw_managed();\n"
3212 "(~s).draw_owned();\n"
3213 "(&s).draw_borrowed();\n"
3218 #: src/doc/tutorial.md:1887
3221 #| "Methods typically take a borrowed pointer self type, so the compiler will "
3222 #| "go to great lengths to convert a callee to a borrowed pointer."
3224 "Methods typically take a reference self type, so the compiler will go to "
3225 "great lengths to convert a callee to a reference."
3227 "多くのメソッドは、借用ポインタの self 型を持つので、コンパイラは呼び出し先を"
3228 "借用ポインタに変換するためあらゆる手段を講じます。"
3231 #: src/doc/tutorial.md:1905
3235 #| "# fn draw_circle(p: Point, f: f64) { }\n"
3236 #| "# fn draw_rectangle(p: Point, p: Point) { }\n"
3237 #| "# struct Point { x: f64, y: f64 }\n"
3238 #| "# enum Shape {\n"
3239 #| "# Circle(Point, f64),\n"
3240 #| "# Rectangle(Point, Point)\n"
3242 #| "# impl Shape {\n"
3243 #| "# fn draw_borrowed(&self) { ... }\n"
3244 #| "# fn draw_managed(@self) { ... }\n"
3245 #| "# fn draw_owned(~self) { ... }\n"
3246 #| "# fn draw_value(self) { ... }\n"
3248 #| "# let s = Circle(Point { x: 1f, y: 2f }, 3f);\n"
3249 #| "// As with typical function arguments, managed and owned pointers\n"
3250 #| "// are automatically converted to borrowed pointers\n"
3253 "# fn draw_circle(p: Point, f: f64) { }\n"
3254 "# fn draw_rectangle(p: Point, p: Point) { }\n"
3255 "# struct Point { x: f64, y: f64 }\n"
3257 "# Circle(Point, f64),\n"
3258 "# Rectangle(Point, Point)\n"
3261 "# fn draw_reference(&self) { ... }\n"
3262 "# fn draw_managed(@self) { ... }\n"
3263 "# fn draw_owned(~self) { ... }\n"
3264 "# fn draw_value(self) { ... }\n"
3266 "# let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);\n"
3267 "// As with typical function arguments, managed and owned pointers\n"
3268 "// are automatically converted to references\n"
3271 "# fn draw_circle(p: Point, f: f64) { }\n"
3272 "# fn draw_rectangle(p: Point, p: Point) { }\n"
3273 "# struct Point { x: f64, y: f64 }\n"
3275 "# Circle(Point, f64),\n"
3276 "# Rectangle(Point, Point)\n"
3279 "# fn draw_borrowed(&self) { ... }\n"
3280 "# fn draw_managed(@self) { ... }\n"
3281 "# fn draw_owned(~self) { ... }\n"
3282 "# fn draw_value(self) { ... }\n"
3284 "# let s = Circle(Point { x: 1f, y: 2f }, 3f);\n"
3285 "// 関数の引数と同様、マネージドポインタと所有ポインタは、\n"
3286 "// 自動的に借用ポインタに変換される\n"
3289 #: src/doc/tutorial.md:1908
3291 #| msgid "(@s).draw_borrowed(); (~s).draw_borrowed();"
3292 msgid "(@s).draw_reference(); (~s).draw_reference();"
3294 "(@s).draw_borrowed();\n"
3295 "(~s).draw_borrowed();"
3298 #: src/doc/tutorial.md:1912
3301 #| "// Unlike typical function arguments, the self value will // "
3302 #| "automatically be referenced ... s.draw_borrowed();"
3304 "// Unlike typical function arguments, the self value will // automatically "
3305 "be referenced ... s.draw_reference();"
3307 "// 関数の引数とは異なり、 self の値は自動的にリファレンスされたり、 ...\n"
3308 "s.draw_borrowed();"
3311 #: src/doc/tutorial.md:1915
3313 #| msgid "// ... and dereferenced (& &s).draw_borrowed();"
3314 msgid "// ... and dereferenced (& &s).draw_reference();"
3316 "// ... デリファレンスされたり、\n"
3317 "(& &s).draw_borrowed();"
3320 #: src/doc/tutorial.md:1919
3322 #| msgid "// ... and dereferenced and borrowed (&@~s).draw_borrowed(); ~~~"
3323 msgid "// ... and dereferenced and borrowed (&@~s).draw_reference(); ~~~"
3325 "// ... デリファレンス後借用されたりします\n"
3326 "(&@~s).draw_borrowed();\n"
3330 #: src/doc/tutorial.md:1923
3332 "Implementations may also define standalone (sometimes called \"static\") "
3333 "methods. The absence of a `self` parameter distinguishes such methods. "
3334 "These methods are the preferred way to define constructor functions."
3336 "実装では、スタンドアロンなメソッド (「静的 (static)」メソッドと呼ばれる場合も"
3337 "あります)を定義することも可能です。引数に `self` をつけない場合、スタンドアロ"
3338 "ンなメソッドとなります。コンストラクタ関数は、スタンドアロンなメソッドとして"
3342 #: src/doc/tutorial.md:1932
3344 "To call such a method, just prefix it with the type name and a double colon:"
3346 "メソッド名の前に型名と2つのコロンを付けることで、スタンドアロンメソッドは呼び"
3350 #: src/doc/tutorial.md:1943
3355 #: src/doc/tutorial.md:1951
3357 "Throughout this tutorial, we've been defining functions that act only on "
3358 "specific data types. With type parameters we can also define functions whose "
3359 "arguments have generic types, and which can be invoked with a variety of "
3360 "types. Consider a generic `map` function, which takes a function `function` "
3361 "and a vector `vector` and returns a new vector consisting of the result of "
3362 "applying `function` to each element of `vector`:"
3364 "このチュートリアルでは、特定のデータ型のみに対して動作する関数を定義してきま"
3365 "した。型パラメータを用いるとジェネリックな型を引数にとり、様々な型で呼び出す"
3366 "ことの可能な関数を定義できます。関数 `function` とベクタ `vector` を引数にと"
3367 "り、`function` を`vector` の各要素に適用した結果からなる新たなベクタを返す、"
3368 "`map` というジェネリック関数について考えます。"
3371 #: src/doc/tutorial.md:1966
3373 "When defined with type parameters, as denoted by `<T, U>`, this function can "
3374 "be applied to any type of vector, as long as the type of `function`'s "
3375 "argument and the type of the vector's contents agree with each other."
3377 "上記例で `<T, U>` と示されているように、型パラメータとともに関数を定義するこ"
3378 "とで、`function` の引数の型と`vectror` の要素の型が一致する場合に限りますが、"
3379 "任意の型のベクタを引数として渡すことが可能になります。"
3382 #: src/doc/tutorial.md:1976
3384 "Inside a generic function, the names of the type parameters (capitalized by "
3385 "convention) stand for opaque types. All you can do with instances of these "
3386 "types is pass them around: you can't apply any operations to them or pattern-"
3387 "match on them. Note that instances of generic types are often passed by "
3388 "pointer. For example, the parameter `function()` is supplied with a pointer "
3389 "to a value of type `T` and not a value of type `T` itself. This ensures that "
3390 "the function works with the broadest set of types possible, since some types "
3391 "are expensive or illegal to copy and pass by value."
3393 "ジェネリック関数の内部では、型パラメータの名前 (慣例的に大文字で表されます) "
3394 "は不透明型 (opaque type) を意味します。これらの型のインスタンスに対しては、他"
3395 "の関数に渡すことだけが可能で、演算子を適用したり、パターンマッチすることはで"
3396 "きません。ジェネリック型のインスタンスは、ポインタにより渡されることもあるこ"
3397 "とに注意してください。例えば、 `function()` パラメータが `T` 型自身の値ではな"
3398 "く `T` 型の値へのポインタとして渡されるということです。いくつかの型は、値をコ"
3399 "ピーするコストが高かったり、コピーが禁じられていたりするので、ポインタ渡しに"
3400 "することで、関数の引数としてとることのできる型の範囲が広がります。"
3403 #: src/doc/tutorial.md:1978
3405 "Generic `type`, `struct`, and `enum` declarations follow the same pattern:"
3407 "ジェネリックな型、構造体、および列挙型の宣言は、同じパターンに従います。"
3410 #: src/doc/tutorial.md:1982
3411 msgid "~~~~ use std::hashmap::HashMap; type Set<T> = HashMap<T, ()>;"
3414 "use std::hashmap::HashMap;\n"
3415 "type Set<T> = HashMap<T, ()>;"
3418 #: src/doc/tutorial.md:1995
3420 "These declarations can be instantiated to valid types like `Set<int>`, "
3421 "`Stack<int>`, and `Option<int>`."
3423 "これらの宣言により、 `Set<int>` や `Stack<int>`、 `Option<int>` のような正当"
3427 #: src/doc/tutorial.md:2001
3429 "The last type in that example, `Option`, appears frequently in Rust code. "
3430 "Because Rust does not have null pointers (except in unsafe code), we need "
3431 "another way to write a function whose result isn't defined on every possible "
3432 "combination of arguments of the appropriate types. The usual way is to write "
3433 "a function that returns `Option<T>` instead of `T`."
3435 "最後の例の `Option` 型は、Rust のコード中に頻繁に現れます。Rust には null ポ"
3436 "インタが存在しない (unsafe なコードを除く) ため、引数の組み合わせがとりうるす"
3437 "べての値に対し結果が定義されないような関数を記述するための別の方法が必要で"
3438 "す。このような場合には、 `T` 型ではなく `Option<T>` 型を返すよう関数を定義す"
3442 #: src/doc/tutorial.md:2020
3444 "The Rust compiler compiles generic functions very efficiently by "
3445 "*monomorphizing* them. *Monomorphization* is a fancy name for a simple idea: "
3446 "generate a separate copy of each generic function at each call site, a copy "
3447 "that is specialized to the argument types and can thus be optimized "
3448 "specifically for them. In this respect, Rust's generics have similar "
3449 "performance characteristics to C++ templates."
3451 "Rust のコンパイラは、ジェネリック関数を *monomorphizing* することで効率的にコ"
3452 "ンパイルします。*monomorphization* という名前は大げさに聞こえますが、考え方は"
3453 "単純です。ジェネリック関数のコピーを関数の呼び出し箇所に別々に生成し、引数の"
3454 "型により特殊化された各コピーは、それぞれの特性に応じて最適化が施されます。こ"
3455 "の点において、Rust のジェネリクスは C++ のテンプレートと似たパフォーマンス上"
3459 #: src/doc/tutorial.md:2022
3464 #: src/doc/tutorial.md:2034
3467 #| "Within a generic function the operations available on generic types are "
3468 #| "very limited. After all, since the function doesn't know what types it is "
3469 #| "operating on, it can't safely modify or query their values. This is where "
3470 #| "_traits_ come into play. Traits are Rust's most powerful tool for writing "
3471 #| "polymorphic code. Java developers will see them as similar to Java "
3472 #| "interfaces, and Haskellers will notice their similarities to type "
3473 #| "classes. Rust's traits are a form of *bounded polymorphism*: a trait is a "
3474 #| "way of limiting the set of possible types that a type parameter could "
3477 "Within a generic function -- that is, a function parameterized by a type "
3478 "parameter, say, `T` -- the operations we can do on arguments of type `T` are "
3479 "quite limited. After all, since we don't know what type `T` will be "
3480 "instantiated with, we can't safely modify or query values of type `T`. This "
3481 "is where _traits_ come into play. Traits are Rust's most powerful tool for "
3482 "writing polymorphic code. Java developers will see them as similar to Java "
3483 "interfaces, and Haskellers will notice their similarities to type classes. "
3484 "Rust's traits give us a way to express *bounded polymorphism*: by limiting "
3485 "the set of possible types that a type parameter could refer to, they expand "
3486 "the number of operations we can safely perform on arguments of that type."
3488 "ジェネリック関数の内部では、ジェネリック型に対して非常に限られた操作しか行え"
3489 "ません。つまるところ、ジェネリック関数は操作の対象とする型が何なのか知らない"
3490 "ため、対象の値を安全に変更・参照することができません。__トレイト__ (_trait_) "
3491 "の出番です。トレイトは Rust でポリモーフィックなコードを書くための最も強力な"
3492 "ツールです。Java 開発者にとってトレイトは Java のインターフェースのように見え"
3493 "ますし、Haskeller は型クラスとの類似点に気づくでしょう。Rust のトレイトは **"
3494 "有界ポリモーフィズム** (*bounded polymorphism*) の形式をとります。トレイトに"
3495 "より、型パラメータが示す得る型の集合を限定することができます。"
3498 #: src/doc/tutorial.md:2041
3501 #| "As motivation, let us consider copying in Rust. The `clone` method is "
3502 #| "not defined for all Rust types. One reason is user-defined destructors: "
3503 #| "copying a type that has a destructor could result in the destructor "
3504 #| "running multiple times. Therefore, types with destructors cannot be "
3505 #| "copied unless you explicitly implement `Clone` for them."
3507 "As motivation, let us consider copying of values in Rust. The `clone` "
3508 "method is not defined for values of every type. One reason is user-defined "
3509 "destructors: copying a value of a type that has a destructor could result in "
3510 "the destructor running multiple times. Therefore, values of types that have "
3511 "destructors cannot be copied unless we explicitly implement `clone` for them."
3513 "トレイト導入の動機となる例として、Rust でのコピーについて考えます。`clone` メ"
3514 "ソッドはすべての Rust の型に対して定義されていません。定義されていない理由の"
3515 "一つとして、ユーザ定義のデストラクタの存在が挙げられます。デストラクタを持つ"
3516 "型をコピーすることで、デストラクタが複数回実行されるという事態を招いてしまう"
3517 "かもしれません。そのため、明示的に `Clone` を実装していない型を除き、デストラ"
3518 "クタをもつ型をコピーすることはできません。"
3521 #: src/doc/tutorial.md:2047
3524 #| "This complicates handling of generic functions. If you have a type "
3525 #| "parameter `T`, can you copy values of that type? In Rust, you can't, and "
3526 #| "if you try to run the following code the compiler will complain."
3528 "This complicates handling of generic functions. If we have a function with "
3529 "a type parameter `T`, can we copy values of type `T` inside that function? "
3530 "In Rust, we can't, and if we try to run the following code the compiler will "
3533 "このことはジェネリック関数の扱い方を複雑にします。型パラメータ `T` が存在した"
3534 "として、この型の値をコピーすることができるでしょうか?Rust では、コピーするこ"
3535 "とはできません。以下のコードを実行しようとしてもコンパイラが文句を言うでしょ"
3539 #: src/doc/tutorial.md:2054
3543 "// This does not compile\n"
3544 "fn head_bad<T>(v: &[T]) -> T {\n"
3545 " v[0] // error: copying a non-copyable value\n"
3550 "// このコードはコンパイルできない\n"
3551 "fn head_bad<T>(v: &[T]) -> T {\n"
3552 " v[0] // error: copying a non-copyable value\n"
3557 #: src/doc/tutorial.md:2060
3560 #| "However, we can tell the compiler that the `head` function is only for "
3561 #| "copyable types: that is, those that implement the `Clone` trait. In that "
3562 #| "case, we can explicitly create a second copy of the value we are "
3563 #| "returning using the `clone` keyword:"
3565 "However, we can tell the compiler that the `head` function is only for "
3566 "copyable types. In Rust, copyable types are those that _implement the "
3567 "`Clone` trait_. We can then explicitly create a second copy of the value we "
3568 "are returning by calling the `clone` method:"
3570 "`head` 関数はコピー可能な型、つまり `Clone` トレイトを実装している型だけを対"
3571 "象にしていることをコンパイラに教えることはできます。この場合、`clone` メソッ"
3572 "ドを使うことで、値のコピーを明示的に作成し、戻り値として返すことができます。"
3575 #: src/doc/tutorial.md:2067
3580 "fn head<T: Clone>(v: &[T]) -> T {\n"
3586 "// このコードはコンパイルできる\n"
3587 "fn head<T: Clone>(v: &[T]) -> T {\n"
3593 #: src/doc/tutorial.md:2077
3596 #| "This says that we can call `head` on any type `T` as long as that type "
3597 #| "implements the `Clone` trait. When instantiating a generic function, you "
3598 #| "can only instantiate it with types that implement the correct trait, so "
3599 #| "you could not apply `head` to a type that does not implement `Clone`."
3601 "The bounded type parameter `T: Clone` says that `head` can be called on an "
3602 "argument of type `&[T]` for any `T`, so long as there is an implementation "
3603 "of the `Clone` trait for `T`. When instantiating a generic function, we can "
3604 "only instantiate it with types that implement the correct trait, so we could "
3605 "not apply `head` to a vector whose elements are of some type that does not "
3606 "implement `Clone`."
3608 "このことは、`Clone` トレイトを実装している任意の型 `T` について、 `head` 関数"
3609 "を呼び出すことができることを示しています。ジェネリック関数を実体化する場合、"
3610 "正しいトレイトを実装した型を用いた場合のみ実体化できます。つまり、`Clone` ト"
3611 "レイトを実装していない型に対して `head` を呼び出すことはできません。"
3614 #: src/doc/tutorial.md:2082
3617 #| "While most traits can be defined and implemented by user code, two traits "
3618 #| "are automatically derived and implemented for all applicable types by the "
3619 #| "compiler, and may not be overridden:"
3621 "While most traits can be defined and implemented by user code, three traits "
3622 "are automatically derived and implemented for all applicable types by the "
3623 "compiler, and may not be overridden:"
3625 "ほとんどのトレイトはユーザコードにより定義・実装できますが、2つのトレイトはコ"
3626 "ンパイラにより自動的に導出され、適用可能なすべての型に対し自動的に実装され、"
3630 #: src/doc/tutorial.md:2086
3633 #| "* `Send` - Sendable types.\n"
3634 #| "Types are sendable\n"
3635 #| "unless they contain managed boxes, managed closures, or borrowed pointers.\n"
3637 "* `Send` - Sendable types.\n"
3638 "Types are sendable\n"
3639 "unless they contain managed boxes, managed closures, or references.\n"
3641 "* `Send` - 送信可能な型。\n"
3642 "マネージドボックスやマネージドクロージャ、借用ポインタを含まない場合、型は送信可能である。"
3645 #: src/doc/tutorial.md:2090
3648 #| "* `Freeze` - Constant (immutable) types.\n"
3649 #| "These are types that do not contain anything intrinsically mutable.\n"
3650 #| "Intrinsically mutable values include `@mut`\n"
3651 #| "and `Cell` in the standard library.\n"
3653 "* `Freeze` - Constant (immutable) types.\n"
3654 "These are types that do not contain anything intrinsically mutable.\n"
3655 "Intrinsically mutable values include `Cell` in the standard library.\n"
3657 "* `Freeze` - 定数 (イミュータブル) 型。\n"
3658 "本質的に変更可能な値を含まない型のことです。本質的に変更可能な値には、`@mut` や標準ライブラリで定義されている `Cell` が含まれます。\n"
3661 #: src/doc/tutorial.md:2099
3663 "> *Note:* These two traits were referred to as 'kinds' in earlier > "
3664 "iterations of the language, and often still are."
3666 "> *注意* これら2つのトレイトは、以前は 「種」 (kind) と呼ばれており、現在"
3670 #: src/doc/tutorial.md:2105
3673 #| "Additionally, the `Drop` trait is used to define destructors. This trait "
3674 #| "defines one method called `drop`, which is automatically called when a "
3675 #| "value of the type that implements this trait is destroyed, either because "
3676 #| "the value went out of scope or because the garbage collector reclaimed it."
3678 "Additionally, the `Drop` trait is used to define destructors. This trait "
3679 "provides one method called `drop`, which is automatically called when a "
3680 "value of the type that implements this trait is destroyed, either because "
3681 "the value went out of scope or because the garbage collector reclaimed it."
3683 "上記に加え、 `Drop` トレイトはデストラクタを定義するために使われます。このト"
3684 "レイトは `drop` というこのトレイトを実装した型が破壊されるタイミング(値がス"
3685 "コープの外に出たタイミングか、ガベージコレクタが回収するタイミング) で呼び出"
3686 "されるメソッドを1つ定義しています。"
3689 #: src/doc/tutorial.md:2110
3691 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
3694 "struct TimeBomb {\n"
3695 " explosivity: uint\n"
3700 "extern crate world;\n"
3701 "fn main() { println(~\"hello \" + world::explore()); }\n"
3705 #: src/doc/tutorial.md:2122
3707 "It is illegal to call `drop` directly. Only code inserted by the compiler "
3710 "`drop` を直接呼び出すことはできません。コンパイラにより挿入されたコードのみ"
3711 "が `drop` を呼び出すことができます。"
3714 #: src/doc/tutorial.md:2124
3715 msgid "## Declaring and implementing traits"
3716 msgstr "## トレイトの宣言と実装"
3719 #: src/doc/tutorial.md:2129
3722 #| "A trait consists of a set of methods without bodies, or may be empty, as "
3723 #| "is the case with `Send` and `Freeze`. For example, we could declare the "
3724 #| "trait `Printable` for things that can be printed to the console, with a "
3727 "At its simplest, a trait is a set of zero or more _method signatures_. For "
3728 "example, we could declare the trait `Printable` for things that can be "
3729 "printed to the console, with a single method signature:"
3731 "トレイトはボディを持たないメソッドの集合から構成されるか、 `Send` や "
3732 "`Freeze` トレイトのように空の場合があります。例えば、コンソールに出力可能なも"
3733 "のを表しメソッドを1つもつ `Printable` トレイトは以下のように宣言できます。"
3736 #: src/doc/tutorial.md:2135
3738 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
3741 "trait Printable {\n"
3742 " fn print(&self);\n"
3748 "extern crate world;\n"
3749 "fn main() { println(~\"hello \" + world::explore()); }\n"
3753 #: src/doc/tutorial.md:2148
3756 #| "Traits may be implemented for specific types with [impls]. An impl that "
3757 #| "implements a trait includes the name of the trait at the start of the "
3758 #| "definition, as in the following impls of `Printable` for `int` and `StrBuf`."
3760 "Traits may be implemented for specific types with [impls]. An impl for a "
3761 "particular trait gives an implementation of the methods that trait "
3762 "provides. For instance, the following impls of `Printable` for `int` and "
3763 "`StrBuf` give implementations of the `print` method."
3765 "[impl][impls] により特定の型にトレイトを実装することができます。トレイトを実"
3766 "装する impl は、以下の `Printable` の `int` と `StrBuf` に対する実装のように、"
3767 "定義の先頭にトレイトの名前を含みます。"
3770 #: src/doc/tutorial.md:2150
3771 msgid "[impls]: #methods"
3772 msgstr "[impls]: #メソッド"
3775 #: src/doc/tutorial.md:2160 src/doc/tutorial.md:2206
3777 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
3779 "impl Printable for StrBuf {\n"
3780 " fn print(&self) { println!(\"{}\", *self) }\n"
3785 "extern crate world;\n"
3786 "fn main() { println(~\"hello \" + world::explore()); }\n"
3790 #: src/doc/tutorial.md:2164
3791 msgid "# 1.print(); # (~\"foo\").print(); ~~~~"
3794 "# (~\"foo\").print();\n"
3798 #: src/doc/tutorial.md:2167
3801 #| "Methods defined in an implementation of a trait may be called just like "
3802 #| "any other method, using dot notation, as in `1.print()`. Traits may "
3803 #| "themselves contain type parameters. A trait for generalized sequence "
3804 #| "types might look like the following:"
3806 "Methods defined in an impl for a trait may be called just like any other "
3807 "method, using dot notation, as in `1.print()`."
3809 "トレイトの実装により定義されたメソッドは他のメソッドと全く同じように、ドット"
3810 "記法を用いて `1.print()` のように呼び出せます。トレイト自体に型パラメータを持"
3811 "たせることもできます。一般化されたシーケンスを表すトレイトは以下のように定義"
3815 #: src/doc/tutorial.md:2169
3817 #| msgid "## Deriving implementations for traits"
3818 msgid "## Default method implementations in trait definitions"
3819 msgstr "## トレイトの実装の導出"
3822 #: src/doc/tutorial.md:2185
3824 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
3826 "impl Printable for bool {\n"
3827 " fn print(&self) { println!(\"{:?}\", *self) }\n"
3832 "extern crate world;\n"
3833 "fn main() { println(~\"hello \" + world::explore()); }\n"
3837 #: src/doc/tutorial.md:2189
3839 #| msgid "# 1.print(); # (~\"foo\").print(); ~~~~"
3840 msgid "# true.print(); # 3.14159.print(); ~~~~"
3843 "# (~\"foo\").print();\n"
3847 #: src/doc/tutorial.md:2216
3849 #| msgid "# 1.print(); # (~\"foo\").print(); ~~~~"
3851 "# 1.print(); # (~\"foo\").print(); # true.print(); # 3.14159.print(); ~~~~"
3854 "# (~\"foo\").print();\n"
3858 #: src/doc/tutorial.md:2227
3860 #| msgid "## Tuples"
3861 msgid "## Type-parameterized traits"
3865 #: src/doc/tutorial.md:2235
3867 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
3871 " fn length(&self) -> uint;\n"
3876 "extern crate world;\n"
3877 "fn main() { println(~\"hello \" + world::explore()); }\n"
3881 #: src/doc/tutorial.md:2240
3883 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
3885 "impl<T> Seq<T> for ~[T] {\n"
3886 " fn length(&self) -> uint { self.len() }\n"
3892 "extern crate world;\n"
3893 "fn main() { println(~\"hello \" + world::explore()); }\n"
3897 #: src/doc/tutorial.md:2247
3899 "The implementation has to explicitly declare the type parameter that it "
3900 "binds, `T`, before using it to specify its trait type. Rust requires this "
3901 "declaration because the `impl` could also, for example, specify an "
3902 "implementation of `Seq<int>`. The trait type (appearing between `impl` and "
3903 "`for`) *refers* to a type, rather than defining one."
3905 "実装が束縛する型パラメータ `T` は、トレイトの型を指定する前に明示的に宣言しな"
3906 "ければなりません。Rust でこの宣言が必要なのは、 `impl` では例えば `Seq<int>` "
3907 "の実装を指定することも可能だからです。トレイトの型 (`impl` と `for` の間に現"
3908 "れるもの) は、型を定義するのではなく、型を **参照** します。"
3911 #: src/doc/tutorial.md:2252
3913 "The type parameters bound by a trait are in scope in each of the method "
3914 "declarations. So, re-declaring the type parameter `T` as an explicit type "
3915 "parameter for `len`, in either the trait or the impl, would be a compile-"
3918 "トレイトにより束縛される型パラメータは、各メソッドの宣言のスコープに属しま"
3919 "す。したがって、trait と impl のいずれかで、型パラメータ `T` を `len` で用い"
3920 "る明示的な型パラメータとして再宣言すると、コンパイル時エラーとなります。"
3923 #: src/doc/tutorial.md:2257
3925 "Within a trait definition, `Self` is a special type that you can think of as "
3926 "a type parameter. An implementation of the trait for any given type `T` "
3927 "replaces the `Self` type parameter with `T`. The following trait describes "
3928 "types that support an equality operation:"
3930 "トレイトの定義内部では、`Self` は型パラメータとみなすことのできる特別な型とな"
3931 "ります。型 `T` に対するトレイトの実装では `Self` は型パラメータ `T` で置き換"
3932 "えられます。以下のトレイトは等価性演算をサポートする型を意味します。"
3935 #: src/doc/tutorial.md:2264
3939 "// In a trait, `self` refers to the self argument.\n"
3940 "// `Self` refers to the type implementing the trait.\n"
3942 " fn equals(&self, other: &Self) -> bool;\n"
3946 "// trait の内側では, `self` は self 引数を指します。\n"
3947 "// `Self` はトレイトを実装する型を指します。\n"
3949 " fn equals(&self, other: &Self) -> bool;\n"
3953 #: src/doc/tutorial.md:2270
3956 "// In an impl, `self` refers just to the value of the receiver\n"
3957 "impl Eq for int {\n"
3958 " fn equals(&self, other: &int) -> bool { *other == *self }\n"
3962 "// impl の内側では `self` はレシーバの値を指します\n"
3963 "impl Eq for int {\n"
3964 " fn equals(&self, other: &int) -> bool { *other == *self }\n"
3969 #: src/doc/tutorial.md:2275
3971 "Notice that in the trait definition, `equals` takes a second parameter of "
3972 "type `Self`. In contrast, in the `impl`, `equals` takes a second parameter "
3973 "of type `int`, only using `self` as the name of the receiver."
3975 "トレイトの定義では、`equals` の第二引数の型は `Self` であることに注意してくだ"
3976 "さい。対照的に `impl` では、 `equals` の第二引数の型は `int` で、 `self` はレ"
3977 "シーバの名前としてのみ使用されます。"
3980 #: src/doc/tutorial.md:2280
3982 "Just as in type implementations, traits can define standalone (static) "
3983 "methods. These methods are called by prefixing the method name with the "
3984 "trait name and a double colon. The compiler uses type inference to decide "
3985 "which implementation to use."
3987 "型への実装の場合と同様、トレイトもスタンドアロン (静的) メソッドを定義するこ"
3988 "とができます。メソッド名の前にトレイト名とコロン2つをつけることで、これらのメ"
3989 "ソッドを呼び出すことができます。コンパイラはどの実装を利用するか決定するた"
3993 #: src/doc/tutorial.md:2286
3996 #| "~~~~ use std::f64::consts::pi; trait Shape { fn new(area: f64) -> Self; } "
3997 #| "struct Circle { radius: f64 } struct Square { length: f64 }"
3999 "~~~~ use std::f64::consts::PI; trait Shape { fn new(area: f64) -> Self; } "
4000 "struct Circle { radius: f64 } struct Square { length: f64 }"
4003 "use std::f64::consts::pi;\n"
4004 "trait Shape { fn new(area: f64) -> Self; }\n"
4005 "struct Circle { radius: f64 }\n"
4006 "struct Square { length: f64 }"
4009 #: src/doc/tutorial.md:2298
4011 "let area = 42.5; let c: Circle = Shape::new(area); let s: Square = Shape::"
4014 "let area = 42.5;\n"
4015 "let c: Circle = Shape::new(area);\n"
4016 "let s: Square = Shape::new(area);\n"
4020 #: src/doc/tutorial.md:2300
4021 msgid "## Bounded type parameters and static method dispatch"
4022 msgstr "## 境界型パラメータと静的メソッドディスパッチ"
4025 #: src/doc/tutorial.md:2305
4027 "Traits give us a language for defining predicates on types, or abstract "
4028 "properties that types can have. We can use this language to define _bounds_ "
4029 "on type parameters, so that we can then operate on generic types."
4031 "トレイトは型の述語 (predicate) や型がもつことのできる抽象的な属性を定義するた"
4032 "めの言語として利用することができます。この言語を使うこととで型パラメータの __"
4033 "境界__ (_bound_) を定義することができ、ジェネリックな型を操作することが可能に"
4037 #: src/doc/tutorial.md:2320
4039 "Declaring `T` as conforming to the `Printable` trait (as we earlier did with "
4040 "`Clone`) makes it possible to call methods from that trait on values of type "
4041 "`T` inside the function. It will also cause a compile-time error when anyone "
4042 "tries to call `print_all` on an array whose element type does not have a "
4043 "`Printable` implementation."
4045 "(先に登場した `Clone` の例のように) `T` が `Printable` トレイトに従うことを宣"
4046 "言することで、関数内部で `T` 型の値に対して `Printable` トレイトのメソッドを"
4047 "呼び出すことが可能になります。また、この宣言により、`Printable` を実装してい"
4048 "ない型を要素とする配列に対して `print_all` 関数を呼びだそうとすると、コンパイ"
4052 #: src/doc/tutorial.md:2323
4054 "Type parameters can have multiple bounds by separating them with `+`, as in "
4055 "this version of `print_all` that copies elements."
4057 "型パラメータは `+` で区切ることで複数の境界を持つことができます。以下の "
4058 "`print_all` は、ベクタの要素をコピーするバージョンです。"
4061 #: src/doc/tutorial.md:2339
4063 "Method calls to bounded type parameters are _statically dispatched_, "
4064 "imposing no more overhead than normal function invocation, so are the "
4065 "preferred way to use traits polymorphically."
4067 "境界型パラメータに対するメソッドの呼び出しは __静的にディスパッチ__ "
4068 "(_statically dispatched_) されるため、通常の関数呼び出し以上のオーバーヘッド"
4069 "は発生しません。そのため、ポリモーフィックにトレイトを使う方法としては、境界"
4073 #: src/doc/tutorial.md:2341
4074 msgid "This usage of traits is similar to Haskell type classes."
4075 msgstr "トレイトのこのような使い方は、Haskell の型クラスと似ています。"
4078 #: src/doc/tutorial.md:2343
4079 msgid "## Trait objects and dynamic method dispatch"
4080 msgstr "## トレイトオブジェクトと動的メソッドディスパッチ"
4083 #: src/doc/tutorial.md:2347
4085 "The above allows us to define functions that polymorphically act on values "
4086 "of a single unknown type that conforms to a given trait. However, consider "
4089 "上述の例では、指定されたトレイトに従う、単一の未知の型の値についてポリモー"
4090 "フィックにふるまう関数を定義しました。ここでは、以下の関数について考えます。"
4093 #: src/doc/tutorial.md:2353
4095 "~~~~ # type Circle = int; type Rectangle = int; # impl Drawable for int { fn "
4096 "draw(&self) {} } # fn new_circle() -> int { 1 } trait Drawable { fn "
4100 "# type Circle = int; type Rectangle = int;\n"
4101 "# impl Drawable for int { fn draw(&self) {} }\n"
4102 "# fn new_circle() -> int { 1 }\n"
4103 "trait Drawable { fn draw(&self); }"
4106 #: src/doc/tutorial.md:2366
4108 "You can call that on an array of circles, or an array of rectangles "
4109 "(assuming those have suitable `Drawable` traits defined), but not on an "
4110 "array containing both circles and rectangles. When such behavior is needed, "
4111 "a trait name can alternately be used as a type, called an _object_."
4113 "円の配列や長方形の配列に対してこの関数を呼び出すことは (円や長方形に対し適切"
4114 "に `Drawable` トレイトが定義されていると仮定すれば) 可能ですが、円や長方形を"
4115 "両方共含む配列に対しては呼び出すことができません。そのような動作が必要な場"
4116 "合、__オブジェクト__ 型として、トレイトの名前を代わりに利用することができま"
4120 #: src/doc/tutorial.md:2378
4122 "In this example, there is no type parameter. Instead, the `@Drawable` type "
4123 "denotes any managed box value that implements the `Drawable` trait. To "
4124 "construct such a value, you use the `as` operator to cast a value to an "
4127 "この例中には、型パラメータはありません。代わりに、「`Drawable` トレイトを実装"
4128 "した、任意のマネージドボックス値」を表す型 `@Drawable` 型があります。このよ"
4129 "うな値を作成するには、 `as` 演算子を使って値をオブジェクトへキャストします。"
4132 #: src/doc/tutorial.md:2385
4134 "~~~~ # type Circle = int; type Rectangle = bool; # trait Drawable { fn "
4135 "draw(&self); } # fn new_circle() -> Circle { 1 } # fn new_rectangle() -> "
4136 "Rectangle { true } # fn draw_all(shapes: &[@Drawable]) {}"
4139 "# type Circle = int; type Rectangle = bool;\n"
4140 "# trait Drawable { fn draw(&self); }\n"
4141 "# fn new_circle() -> Circle { 1 }\n"
4142 "# fn new_rectangle() -> Rectangle { true }\n"
4143 "# fn draw_all(shapes: &[@Drawable]) {}"
4146 #: src/doc/tutorial.md:2388
4148 "impl Drawable for Circle { fn draw(&self) { ... } } impl Drawable for "
4149 "Rectangle { fn draw(&self) { ... } }"
4151 "impl Drawable for Circle { fn draw(&self) { ... } }\n"
4152 "impl Drawable for Rectangle { fn draw(&self) { ... } }"
4155 #: src/doc/tutorial.md:2393
4157 "let c: @Circle = @new_circle(); let r: @Rectangle = @new_rectangle(); "
4158 "draw_all([c as @Drawable, r as @Drawable]); ~~~~"
4160 "let c: @Circle = @new_circle();\n"
4161 "let r: @Rectangle = @new_rectangle();\n"
4162 "draw_all([c as @Drawable, r as @Drawable]);\n"
4166 #: src/doc/tutorial.md:2401
4168 "We omit the code for `new_circle` and `new_rectangle`; imagine that these "
4169 "just return `Circle`s and `Rectangle`s with a default size. Note that, like "
4170 "strings and vectors, objects have dynamic size and may only be referred to "
4171 "via one of the pointer types. Other pointer types work as well. Casts to "
4172 "traits may only be done with compatible pointers so, for example, an "
4173 "`@Circle` may not be cast to an `~Drawable`."
4175 "`new_circle` と `new_rectangle` のコードは省略されていますが、デフォルトのサ"
4176 "イズをもつ `Circle` と `Rectangle` をただ返すものだと考えてください。文字列や"
4177 "ベクタのように、オブジェクトも動的なサイズを持ち、いずれかのポインタ型を経由"
4178 "してしか参照できないことに注意してください。他のポインタ型でもうまく動作しま"
4179 "す。トレイトへのキャストは互換性のあるポインタの場合しか行えません。例えば、"
4180 "`@Cicle` を `~Drawable` にキャストすることはできません。"
4183 #: src/doc/tutorial.md:2415
4185 "~~~ # type Circle = int; type Rectangle = int; # trait Drawable { fn "
4186 "draw(&self); } # impl Drawable for int { fn draw(&self) {} } # fn "
4187 "new_circle() -> int { 1 } # fn new_rectangle() -> int { 2 } // A managed "
4188 "object let boxy: @Drawable = @new_circle() as @Drawable; // An owned object "
4189 "let owny: ~Drawable = ~new_circle() as ~Drawable; // A borrowed object let "
4190 "stacky: &Drawable = &new_circle() as &Drawable; ~~~"
4193 "# type Circle = int; type Rectangle = int;\n"
4194 "# trait Drawable { fn draw(&self); }\n"
4195 "# impl Drawable for int { fn draw(&self) {} }\n"
4196 "# fn new_circle() -> int { 1 }\n"
4197 "# fn new_rectangle() -> int { 2 }\n"
4198 "// A managed object\n"
4199 "let boxy: @Drawable = @new_circle() as @Drawable;\n"
4200 "// An owned object\n"
4201 "let owny: ~Drawable = ~new_circle() as ~Drawable;\n"
4202 "// A borrowed object\n"
4203 "let stacky: &Drawable = &new_circle() as &Drawable;\n"
4207 #: src/doc/tutorial.md:2420
4209 "Method calls to trait types are _dynamically dispatched_. Since the compiler "
4210 "doesn't know specifically which functions to call at compile time, it uses a "
4211 "lookup table (also known as a vtable or dictionary) to select the method to "
4214 "トレイト型のメソッド呼び出しは __動的にディスパッチ__ (_dynamically "
4215 "dispatched_) されます。どの関数を呼び出すべきかコンパイル時にはわからないた"
4216 "め、ルックアップテーブル (vtable や dictionary としても知られている) を用い"
4217 "て、呼び出すメソッドの選択を実行時に行います。"
4220 #: src/doc/tutorial.md:2422
4221 msgid "This usage of traits is similar to Java interfaces."
4222 msgstr "トレイトのこのような使い方は、Java のインターフェースと似ています。"
4225 #: src/doc/tutorial.md:2448
4226 msgid "## Trait inheritance"
4230 #: src/doc/tutorial.md:2453
4232 "We can write a trait declaration that _inherits_ from other traits, called "
4233 "_supertraits_. Types that implement a trait must also implement its "
4234 "supertraits. For example, we can define a `Circle` trait that inherits from "
4237 "__スーパートレイト__ と呼ばれる他のトレイトから __継承した__ トレイトを宣言す"
4238 "ることができます。継承されたトレイトを実装する型は、スーパートレイトも実装し"
4239 "なければなりません。例えば、 `Circle` トレイトを `Shape` トレイトを継承したも"
4243 #: src/doc/tutorial.md:2458
4245 "~~~~ trait Shape { fn area(&self) -> f64; } trait Circle : Shape { fn "
4246 "radius(&self) -> f64; } ~~~~"
4249 "trait Shape { fn area(&self) -> f64; }\n"
4250 "trait Circle : Shape { fn radius(&self) -> f64; }\n"
4254 #: src/doc/tutorial.md:2460
4256 "Now, we can implement `Circle` on a type only if we also implement `Shape`."
4257 msgstr "`Circle` トレイトの実装は、 `Shape` を実装した型についてのみ行えます。"
4260 #: src/doc/tutorial.md:2475
4262 #| msgid "~~~ {.ignore} use std::f64::consts::pi; # trait Shape { fn area(&self) -> f64; } # trait Circle : Shape { fn radius(&self) -> f64; } # struct Point { x: f64, y: f64 } # struct CircleStruct { center: Point, radius: f64 } # impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / pi).sqrt() } } # impl Shape for CircleStruct { fn area(&self) -> f64 { pi * square(self.radius) } }"
4265 "use std::f64::consts::PI;\n"
4266 "# trait Shape { fn area(&self) -> f64; }\n"
4267 "# trait Circle : Shape { fn radius(&self) -> f64; }\n"
4268 "# struct Point { x: f64, y: f64 }\n"
4269 "# fn square(x: f64) -> f64 { x * x }\n"
4270 "struct CircleStruct { center: Point, radius: f64 }\n"
4271 "impl Circle for CircleStruct {\n"
4272 " fn radius(&self) -> f64 { (self.area() / PI).sqrt() }\n"
4274 "impl Shape for CircleStruct {\n"
4275 " fn area(&self) -> f64 { PI * square(self.radius) }\n"
4280 "use std::f64::consts::pi;\n"
4281 "# trait Shape { fn area(&self) -> f64; }\n"
4282 "# trait Circle : Shape { fn radius(&self) -> f64; }\n"
4283 "# struct Point { x: f64, y: f64 }\n"
4284 "# struct CircleStruct { center: Point, radius: f64 }\n"
4285 "# impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / pi).sqrt() } }\n"
4286 "# impl Shape for CircleStruct { fn area(&self) -> f64 { pi * square(self.radius) } }"
4289 #: src/doc/tutorial.md:2480
4291 "Notice that methods of `Circle` can call methods on `Shape`, as our `radius` "
4292 "implementation calls the `area` method. This is a silly way to compute the "
4293 "radius of a circle (since we could just return the `radius` field), but you "
4296 "`radius` の実装から `area` メソッドを呼び出せるように、`Circle` のメソッドは "
4297 "`Shape` のメソッドを呼び出せることに注意してください。(単純に `radius` フィー"
4298 "ルドの値を返すことができるにも関わらず) 円の半径を面積から計算するのは馬鹿げ"
4299 "ていますが、メソッド呼び出しの考え方は分かったでしょう。"
4302 #: src/doc/tutorial.md:2484
4304 "In type-parameterized functions, methods of the supertrait may be called on "
4305 "values of subtrait-bound type parameters. Refering to the previous example "
4306 "of `trait Circle : Shape`:"
4308 "型パラメータを持つ関数では、サブトレイトの境界型パラメータの値によりスーパー"
4309 "トレイトのメソッドを呼び出すことになります。前の例の `trait Circle : Shape` "
4313 #: src/doc/tutorial.md:2493
4317 "# trait Shape { fn area(&self) -> f64; }\n"
4318 "# trait Circle : Shape { fn radius(&self) -> f64; }\n"
4319 "fn radius_times_area<T: Circle>(c: T) -> f64 {\n"
4320 " // `c` is both a Circle and a Shape\n"
4321 " c.radius() * c.area()\n"
4326 "# trait Shape { fn area(&self) -> f64; }\n"
4327 "# trait Circle : Shape { fn radius(&self) -> f64; }\n"
4328 "fn radius_times_area<T: Circle>(c: T) -> f64 {\n"
4329 " // `c` は Circle でもあり、Shape でもある\n"
4330 " c.radius() * c.area()\n"
4335 #: src/doc/tutorial.md:2504
4338 #| "~~~ {.ignore} use std::f64::consts::pi; # trait Shape { fn area(&self) -> "
4339 #| "f64; } # trait Circle : Shape { fn radius(&self) -> f64; } # struct Point "
4340 #| "{ x: f64, y: f64 } # struct CircleStruct { center: Point, radius: f64 } # "
4341 #| "impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / "
4342 #| "pi).sqrt() } } # impl Shape for CircleStruct { fn area(&self) -> f64 { pi "
4343 #| "* square(self.radius) } }"
4345 "~~~ {.ignore} use std::f64::consts::PI; # trait Shape { fn area(&self) -> "
4346 "f64; } # trait Circle : Shape { fn radius(&self) -> f64; } # struct Point "
4347 "{ x: f64, y: f64 } # struct CircleStruct { center: Point, radius: f64 } # "
4348 "impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / PI)."
4349 "sqrt() } } # impl Shape for CircleStruct { fn area(&self) -> f64 { PI * "
4350 "square(self.radius) } }"
4353 "use std::f64::consts::pi;\n"
4354 "# trait Shape { fn area(&self) -> f64; }\n"
4355 "# trait Circle : Shape { fn radius(&self) -> f64; }\n"
4356 "# struct Point { x: f64, y: f64 }\n"
4357 "# struct CircleStruct { center: Point, radius: f64 }\n"
4358 "# impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / "
4360 "# impl Shape for CircleStruct { fn area(&self) -> f64 { pi * square(self."
4364 #: src/doc/tutorial.md:2509
4366 "let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f}; let "
4367 "mycircle: @Circle = concrete as @Circle; let nonsense = mycircle.radius() * "
4368 "mycircle.area(); ~~~"
4370 "let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f};\n"
4371 "let mycircle: @Circle = concrete as @Circle;\n"
4372 "let nonsense = mycircle.radius() * mycircle.area();\n"
4376 #: src/doc/tutorial.md:2511
4377 msgid "> *Note:* Trait inheritance does not actually work with objects yet"
4379 "> *注意* トレイトの継承は、実際にはまだオブジェクトに対しては動作しませ"
4383 #: src/doc/tutorial.md:2513
4384 msgid "## Deriving implementations for traits"
4385 msgstr "## トレイトの実装の導出"
4388 #: src/doc/tutorial.md:2520
4390 "A small number of traits in `std` and `extra` can have implementations that "
4391 "can be automatically derived. These instances are specified by placing the "
4392 "`deriving` attribute on a data type declaration. For example, the following "
4393 "will mean that `Circle` has an implementation for `Eq` and can be used with "
4394 "the equality operators, and that a value of type `ABC` can be randomly "
4395 "generated and converted to a string:"
4397 "`std` と `extra` で定義されているいくつかのトレイトは、自動的に実装を導出可能"
4398 "です。データ型の宣言時に `deriving` 属性を書くことで、これらのトレイトを実装"
4399 "することを指定します。例えば、以下の例では `Circle` は `Eq` を実装し等価演算"
4400 "子で比較することが可能であり、`ABC` 型の値はランダムに生成することや、文字列"
4404 #: src/doc/tutorial.md:2524
4405 msgid "~~~ #[deriving(Eq)] struct Circle { radius: f64 }"
4409 "struct Circle { radius: f64 }"
4412 #: src/doc/tutorial.md:2528
4413 msgid "#[deriving(Rand, ToStr)] enum ABC { A, B, C } ~~~"
4415 "#[deriving(Rand, ToStr)]\n"
4416 "enum ABC { A, B, C }\n"
4420 #: src/doc/tutorial.md:2532
4423 #| "The full list of derivable traits is `Eq`, `TotalEq`, `Ord`, `TotalOrd`, "
4424 #| "`Encodable` `Decodable`, `Clone`, `DeepClone`, `Hash`, `Rand`, "
4425 #| "`Zero`, and `ToStr`."
4427 "The full list of derivable traits is `Eq`, `TotalEq`, `Ord`, `TotalOrd`, "
4428 "`Encodable` `Decodable`, `Clone`, `DeepClone`, `Hash`, `Rand`, "
4429 "`Default`, `Zero`, and `ToStr`."
4431 "実装を自動的に導出可能なトレイトは、 `Eq`, `TotalEq`, `Ord`, `TotalOrd`, "
4432 "`Encodable` `Decodable`, `Clone`, `DeepClone`, `Hash`, `Rand`, `Zero`, "
4436 #: src/doc/tutorial.md:2534
4438 #| msgid "# Modules and crates"
4439 msgid "# Crates and the module system"
4440 msgstr "# モジュールとクレート"
4443 #: src/doc/tutorial.md:2539
4448 #: src/doc/tutorial.md:2554
4450 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4455 " println!(\"Hello world!\");\n"
4461 "extern crate world;\n"
4462 "fn main() { println(~\"hello \" + world::explore()); }\n"
4466 #: src/doc/tutorial.md:2557
4469 #| "The unit of independent compilation in Rust is the crate: rustc compiles "
4470 #| "a single crate at a time, from which it produces either a library or an "
4473 "A crate is also the unit of independent compilation in Rust: `rustc` always "
4474 "compiles a single crate at a time, from which it produces either a library "
4477 "Rust の独立コンパイルの単位はクレートです。rustc は1度に1つのクレートをコンパ"
4478 "イルし、ライブラリか実行可能ファイルを生成します。"
4481 #: src/doc/tutorial.md:2561
4483 #| msgid "## The standard library"
4484 msgid "## The module hierarchy"
4488 #: src/doc/tutorial.md:2587
4490 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4493 " println!(\"Hello farm!\");\n"
4499 "extern crate world;\n"
4500 "fn main() { println(~\"hello \" + world::explore()); }\n"
4504 #: src/doc/tutorial.md:2595
4506 #| msgid "## Managed closures"
4507 msgid "## Paths and visibility"
4508 msgstr "## マネージドクロージャ"
4511 #: src/doc/tutorial.md:2607
4513 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4516 " println!(\"Hello chicken!\");\n"
4520 "extern crate world;\n"
4521 "fn main() { println(~\"hello \" + world::explore()); }\n"
4525 #: src/doc/tutorial.md:2639
4527 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4530 " println!(\"Hello chicken!\");\n"
4531 " ::farm::chicken(); // This compiles now\n"
4537 "extern crate world;\n"
4538 "fn main() { println(~\"hello \" + world::explore()); }\n"
4542 #: src/doc/tutorial.md:2646
4545 #| "Visibility restrictions in Rust exist only at module boundaries. This is "
4546 #| "quite different from most object-oriented languages that also enforce "
4547 #| "restrictions on objects themselves. That's not to say that Rust doesn't "
4548 #| "support encapsulation: both struct fields and methods can be private. But "
4549 #| "this encapsulation is at the module level, not the struct level. Note "
4550 #| "that fields and methods are _public_ by default."
4552 "Visibility restrictions in Rust exist only at module boundaries. This is "
4553 "quite different from most object-oriented languages that also enforce "
4554 "restrictions on objects themselves. That's not to say that Rust doesn't "
4555 "support encapsulation: both struct fields and methods can be private. But "
4556 "this encapsulation is at the module level, not the struct level."
4558 "Rust での可視性の制約はモジュールの境界にのみ存在します。これは、オブジェクト"
4559 "自体にも可視性の制約を課すほとんどのオブジェクト指向言語とは全く異なっていま"
4560 "す。しかし、Rust がカプセル化をサポートしていないというわけではありません。構"
4561 "造体のフィールドとメソッドはプライベートにすることができます。しかし、このカ"
4562 "プセル化はモジュールレベルで働くもので、構造体のレベルで働くものではありませ"
4563 "ん。フィールドとメソッドでは、デフォルトでは __パブリック__ であることに注意"
4567 #: src/doc/tutorial.md:2689
4569 #| msgid "# Modules and crates"
4570 msgid "## Files and modules"
4571 msgstr "# モジュールとクレート"
4574 #: src/doc/tutorial.md:2719
4576 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4579 " println!(\"Hello farm!\");\n"
4586 "extern crate world;\n"
4587 "fn main() { println(~\"hello \" + world::explore()); }\n"
4591 #: src/doc/tutorial.md:2755
4593 #| msgid "~~~~ {.ignore} let foo = 10;"
4596 "// `src/main.rs`\n"
4610 #: src/doc/tutorial.md:2761 src/doc/tutorial.md:2793
4612 #| msgid "~~~~ {.ignore} let foo = 10;"
4613 msgid "~~~ {.notrust} src/plants.rs src/plants/mod.rs"
4619 #: src/doc/tutorial.md:2778
4621 #| msgid "~~~~ {.ignore} let foo = 10;"
4622 msgid "~~~ {.ignore} // `src/main.rs` mod plants; mod animals; ~~~"
4628 #: src/doc/tutorial.md:2786
4630 #| msgid "~~~~ {.ignore} let foo = 10;"
4633 "// `src/animals.rs` or `src/animals/mod.rs`\n"
4644 #: src/doc/tutorial.md:2820
4646 #| msgid "~~~~ {.ignore} let foo = 10;"
4647 msgid "~~~ {.ignore} #[path=\"../../area51/alien.rs\"] mod classified; ~~~"
4653 #: src/doc/tutorial.md:2897
4656 #| "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello "
4657 #| "\" + world::explore()); } ~~~~"
4658 msgid "fn main() { cow() } ~~~"
4662 "extern crate world;\n"
4663 "fn main() { println(~\"hello \" + world::explore()); }\n"
4667 #: src/doc/tutorial.md:2916
4669 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4672 " println!(\"Hello farm!\");\n"
4676 "extern crate world;\n"
4677 "fn main() { println(~\"hello \" + world::explore()); }\n"
4681 #: src/doc/tutorial.md:2923
4683 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4685 " // Can now refer to those names directly:\n"
4694 "extern crate world;\n"
4695 "fn main() { println(~\"hello \" + world::explore()); }\n"
4699 #: src/doc/tutorial.md:2932
4702 #| "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello "
4703 #| "\" + world::explore()); } ~~~~"
4705 "~~~{.ignore} // `a.rs` - crate root use b::foo; mod b; fn main() { foo(); } "
4710 "extern crate world;\n"
4711 "fn main() { println(~\"hello \" + world::explore()); }\n"
4715 #: src/doc/tutorial.md:2939
4718 #| "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello "
4719 #| "\" + world::explore()); } ~~~~"
4721 "~~~{.ignore} // `b.rs` use b::c::bar; pub mod c; pub fn foo() { bar(); } ~~~"
4725 "extern crate world;\n"
4726 "fn main() { println(~\"hello \" + world::explore()); }\n"
4730 #: src/doc/tutorial.md:2985
4732 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4741 "extern crate world;\n"
4742 "fn main() { println(~\"hello \" + world::explore()); }\n"
4746 #: src/doc/tutorial.md:2991
4748 #| msgid "# Dereferencing pointers"
4749 msgid "## Reexporting names"
4750 msgstr "# ポインタのデリファレンス"
4753 #: src/doc/tutorial.md:3014
4755 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4758 " farm::chicken();\n"
4766 "extern crate world;\n"
4767 "fn main() { println(~\"hello \" + world::explore()); }\n"
4771 #: src/doc/tutorial.md:3026
4773 #| msgid "## Using other crates"
4774 msgid "## Using libraries"
4775 msgstr "## 他のクレートの利用"
4778 #: src/doc/tutorial.md:3035
4780 #| msgid "## Using other crates"
4781 msgid "For that, Rust offers you the `extern crate` declaration:"
4782 msgstr "## 他のクレートの利用"
4785 #: src/doc/tutorial.md:3045
4787 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4790 " // The rational number '1/2':\n"
4791 " let one_half = ::extra::rational::Ratio::new(1, 2);\n"
4797 "extern crate world;\n"
4798 "fn main() { println(~\"hello \" + world::explore()); }\n"
4802 #: src/doc/tutorial.md:3068
4804 #| msgid "~~~~ {.ignore} let foo = 10;"
4805 msgid "~~~ extern crate extra;"
4811 #: src/doc/tutorial.md:3081
4813 #| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
4817 " let a_third = Ratio::new(1, 3);\n"
4823 "extern crate world;\n"
4824 "fn main() { println(~\"hello \" + world::explore()); }\n"
4828 #: src/doc/tutorial.md:3086
4830 #| msgid "## Structs"
4831 msgid "## Package ids"
4835 #: src/doc/tutorial.md:3123
4838 #| "~~~~ {.ignore} extern crate farm; extern crate my_farm (name = \"farm\", vers "
4839 #| "= \"2.5\"); extern crate my_auxiliary_farm (name = \"farm\", author = \"mjh"
4842 "~~~~ {.ignore} extern crate farm; extern crate farm = \"farm#2.5\"; extern crate "
4843 "my_farm = \"farm\"; ~~~~"
4846 "extern crate farm;\n"
4847 "extern crate my_farm (name = \"farm\", vers = \"2.5\");\n"
4848 "extern crate my_auxiliary_farm (name = \"farm\", author = \"mjh\");\n"
4852 #: src/doc/tutorial.md:3134
4854 #| msgid "// Make a library (\"bin\" is the default) #![crate_type = \"lib\"]"
4856 "// This crate is a library (\"bin\" is the default) #[crate_id = "
4857 "\"farm#2.5\"]; #![crate_type = \"lib\"]"
4859 "// ライブラリを作成する (\"bin\" がデフォルト値)\n"
4860 "#![crate_type = \"lib\"]"
4863 #: src/doc/tutorial.md:3139
4865 #| msgid "// Turn on a warning #[warn(non_camel_case_types)]"
4866 msgid "// Turn on a warning #[warn(non_camel_case_types)] # fn farm() {} ~~~~"
4869 "#[warn(non_camel_case_types)]"
4872 #: src/doc/tutorial.md:3141
4873 msgid "## A minimal example"
4877 #: src/doc/tutorial.md:3143
4880 #| "Now for something that you can actually compile yourself, we have these "
4882 msgid "Now for something that you can actually compile yourself."
4884 "あなた自身で実際にコンパイルが行える例として、以下の2つのファイルを例示しま"
4888 #: src/doc/tutorial.md:3153
4891 #| "~~~~ // world.rs #![link(name = \"world\", vers = \"1.0\")] pub fn "
4892 #| "explore() -> &str { \"world\" } ~~~~"
4894 "~~~~ // `world.rs` #![crate_id = \"world#0.42\"] # extern crate extra; pub fn "
4895 "explore() -> &'static str { \"world\" } # fn main() {} ~~~~"
4899 "#![link(name = \"world\", vers = \"1.0\")]\n"
4900 "pub fn explore() -> &str { \"world\" }\n"
4904 #: src/doc/tutorial.md:3159
4907 #| "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello "
4908 #| "\" + world::explore()); } ~~~~"
4910 "~~~~ {.ignore} // `main.rs` extern crate world; fn main() { println!(\"hello "
4911 "{}\", world::explore()); } ~~~~"
4915 "extern crate world;\n"
4916 "fn main() { println(~\"hello \" + world::explore()); }\n"
4920 #: src/doc/tutorial.md:3161
4921 msgid "Now compile and run like this (adjust to your platform if necessary):"
4923 "以下のようにコンパイルし、実行します (必要であれば、お使いのプラットフォーム"
4927 #: src/doc/tutorial.md:3168
4930 #| "~~~~ {.notrust}\n"
4931 #| "> rustc --lib world.rs # compiles libworld-94839cbfe144198-1.0.so\n"
4932 #| "> rustc main.rs -L . # compiles main\n"
4934 #| "\"hello world\"\n"
4938 "> rustc --lib world.rs # compiles libworld-<HASH>-0.42.so\n"
4939 "> rustc main.rs -L . # compiles main\n"
4945 "> rustc --lib world.rs # libworld-94839cbfe144198-1.0.so が生成される\n"
4946 "> rustc main.rs -L . # main が生成される\n"
4952 #: src/doc/tutorial.md:3173
4955 #| "Notice that the library produced contains the version in the filename as "
4956 #| "well as an inscrutable string of alphanumerics. These are both part of "
4957 #| "Rust's library versioning scheme. The alphanumerics are a hash "
4958 #| "representing the crate metadata."
4960 "Notice that the library produced contains the version in the file name as "
4961 "well as an inscrutable string of alphanumerics. As explained in the previous "
4962 "paragraph, these are both part of Rust's library versioning scheme. The "
4963 "alphanumerics are a hash representing the crates package ID."
4965 "生成されたライブラリのファイル名には、バージョン番号だけでなく謎めいた英数字"
4966 "が含まれていることに注意してください。これらは両方共 Rust のバージョン管理ス"
4967 "キームの一員です。英数字は、クレートのメタデータを表すハッシュ値です。"
4970 #: src/doc/tutorial.md:3175
4972 #| msgid "## The standard library"
4973 msgid "## The standard library and the prelude"
4977 #: src/doc/tutorial.md:3187
4979 #| msgid "~~~~ {.ignore} let foo = 10;"
4980 msgid "~~~ {.ignore} extern crate std; ~~~"
4986 #: src/doc/tutorial.md:3193
4988 #| msgid "~~~~ {.ignore} let foo = 10;"
4989 msgid "~~~ {.ignore} use std::prelude::*; ~~~"
4995 #: src/doc/tutorial.md:3234
4997 #| msgid "## The standard library"
4998 msgid "## The extra library"
5002 #: src/doc/tutorial.md:3246
5003 msgid "# What next?"
5007 #: src/doc/tutorial.md:3249
5010 #| "Now that you know the essentials, check out any of the additional "
5011 #| "tutorials on individual topics."
5013 "Now that you know the essentials, check out any of the additional guides on "
5014 "individual topics."
5016 "Rust の本質的な部分に関する説明は以上です。個々のトピックにおける追加のチュー"
5017 "トリアルもチェックしてみてください。"
5019 #. type: Bullet: '* '
5020 #: src/doc/tutorial.md:3260
5022 #| msgid "[Containers and iterators](tutorial-container.html)"
5023 msgid "[Pointers][pointers]"
5024 msgstr "[コンテナとイテレータ](tutorial-container.html)"
5026 #. type: Bullet: '* '
5027 #: src/doc/tutorial.md:3260
5029 #| msgid "% The Rust Language Tutorial"
5030 msgid "[Lifetimes][lifetimes]"
5031 msgstr "% Rust 言語チュートリアル"
5033 #. type: Bullet: '* '
5034 #: src/doc/tutorial.md:3260
5035 msgid "[Tasks and communication][tasks]"
5036 msgstr "[タスクと通信][tasks]"
5038 #. type: Bullet: '* '
5039 #: src/doc/tutorial.md:3260
5040 msgid "[Macros][macros]"
5041 msgstr "[マクロ][macros]"
5043 #. type: Bullet: '* '
5044 #: src/doc/tutorial.md:3260
5045 msgid "[The foreign function interface][ffi]"
5046 msgstr "[他言語間インターフェース (foreign function inferface)][ffi]"
5048 #. type: Bullet: '* '
5049 #: src/doc/tutorial.md:3260
5051 #| msgid "[Containers and iterators](tutorial-container.html)"
5052 msgid "[Containers and iterators][container]"
5053 msgstr "[コンテナとイテレータ](tutorial-container.html)"
5055 #. type: Bullet: '* '
5056 #: src/doc/tutorial.md:3260
5058 #| msgid "% The Rust Language Tutorial"
5059 msgid "[Testing Rust code][testing]"
5060 msgstr "% Rust 言語チュートリアル"
5062 #. type: Bullet: '* '
5063 #: src/doc/tutorial.md:3260
5065 #| msgid "% The Rust Language Tutorial"
5066 msgid "[The Rust Runtime][runtime]"
5067 msgstr "% Rust 言語チュートリアル"