From 3bc1b3714453227ea33e8052caa7459f69c96ab6 Mon Sep 17 00:00:00 2001 From: Yoshito Komatsu Date: Thu, 28 Jan 2016 06:29:58 +0000 Subject: [PATCH 1/7] Update functions --- 1.6/ja/book/functions.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/1.6/ja/book/functions.md b/1.6/ja/book/functions.md index 84cea5da..ebead780 100644 --- a/1.6/ja/book/functions.md +++ b/1.6/ja/book/functions.md @@ -1,4 +1,5 @@ -% Functions +# 関数 + Every Rust program has at least one function, the `main` function: From 2f8ab8bd1ad893f65ca0800b6c3758d142cfc23a Mon Sep 17 00:00:00 2001 From: Yoshito Komatsu Date: Thu, 28 Jan 2016 07:07:47 +0000 Subject: [PATCH 2/7] Update functions --- 1.6/ja/book/functions.md | 252 +++++++++++++++++++++++++-------------- 1 file changed, 161 insertions(+), 91 deletions(-) diff --git a/1.6/ja/book/functions.md b/1.6/ja/book/functions.md index ebead780..5b0eff8f 100644 --- a/1.6/ja/book/functions.md +++ b/1.6/ja/book/functions.md @@ -1,24 +1,30 @@ # 関数 -Every Rust program has at least one function, the `main` function: + +Rustのプログラムには全て、少なくとも1つの関数、`main`関数があります。 ```rust fn main() { } ``` -This is the simplest possible function declaration. As we mentioned before, -`fn` says ‘this is a function’, followed by the name, some parentheses because -this function takes no arguments, and then some curly braces to indicate the -body. Here’s a function named `foo`: + + + + +これは評価可能な関数定義の最も単純なものです。 +前に言ったように、`fn`は「これは関数です」ということを示します。この関数には引数がないので、名前と丸括弧が続きます。そして、その本文を表す波括弧が続きます。 +これが`foo`という名前の関数です。 ```rust fn foo() { } ``` -So, what about taking arguments? Here’s a function that prints a number: + +それでは、引数を取る場合はどうでしょうか。 +これが数値を出力する関数です。 ```rust fn print_number(x: i32) { @@ -26,7 +32,8 @@ fn print_number(x: i32) { } ``` -Here’s a complete program that uses `print_number`: + +これが`print_number`を使う完全なプログラムです。 ```rust fn main() { @@ -38,10 +45,13 @@ fn print_number(x: i32) { } ``` -As you can see, function arguments work very similar to `let` declarations: -you add a type to the argument name, after a colon. + + +見てのとおり、関数の引数は`let`宣言と非常によく似た動きをします。 +引数の名前にコロンに続けて型を追加します。 -Here’s a complete program that adds two numbers together and prints them: + +これが2つの数値を足して結果を出力する完全なプログラムです。 ```rust fn main() { @@ -53,11 +63,14 @@ fn print_sum(x: i32, y: i32) { } ``` -You separate arguments with a comma, both when you call the function, as well -as when you declare it. + + +関数を呼び出すときも、それを宣言したときと同様に、引数をコンマで区切ります。 -Unlike `let`, you _must_ declare the types of function arguments. This does -not work: + + +`let`と異なり、あなたは関数の引数の型を宣言 _しなければなりません_ 。 +これは動きません。 ```rust,ignore fn print_sum(x, y) { @@ -65,20 +78,26 @@ fn print_sum(x, y) { } ``` -You get this error: + +このエラーが発生します。 ```text expected one of `!`, `:`, or `@`, found `)` fn print_number(x, y) { ``` -This is a deliberate design decision. While full-program inference is possible, -languages which have it, like Haskell, often suggest that documenting your -types explicitly is a best-practice. We agree that forcing functions to declare -types while allowing for inference inside of function bodies is a wonderful -sweet spot between full inference and no inference. + + + + + +これはよく考えられた設計上の決断です。 +プログラムによる完全な推論は可能ですが、Haskellのようにそれを行っている言語では、しばしば型を明示的にドキュメント化することがベストプラクティスであるとして提案されます。 +私たちは関数に型の宣言を強制する一方で、関数の本文での推論を認めることが完全な推論と推論なしとの間のすばらしいスイートスポットであるということで意見が一致したのです。 -What about returning a value? Here’s a function that adds one to an integer: + +戻り値についてはどうでしょうか。 +これが整数に1を加える関数です。 ```rust fn add_one(x: i32) -> i32 { @@ -86,10 +105,14 @@ fn add_one(x: i32) -> i32 { } ``` -Rust functions return exactly one value, and you declare the type after an -‘arrow’, which is a dash (`-`) followed by a greater-than sign (`>`). The last -line of a function determines what it returns. You’ll note the lack of a -semicolon here. If we added it in: + + + + +Rustの関数はちょうど1つだけの値を返します。そして、ダッシュ(`-`)の後ろに大なりの記号(`>`)を続けた「矢印」の後にその型を宣言します。 +関数の最後の行が何を戻すのかを決定します。 +ここにセミコロンがないことに気が付くでしょう。 +もしそれを追加すると、こうなります。 ```rust,ignore fn add_one(x: i32) -> i32 { @@ -97,7 +120,8 @@ fn add_one(x: i32) -> i32 { } ``` -We would get an error: + +エラーが発生するでしょう。 ```text error: not all control paths return a value @@ -110,44 +134,62 @@ help: consider removing this semicolon: ^ ``` -This reveals two interesting things about Rust: it is an expression-based -language, and semicolons are different from semicolons in other ‘curly brace -and semicolon’-based languages. These two things are related. - -## Expressions vs. Statements - -Rust is primarily an expression-based language. There are only two kinds of -statements, and everything else is an expression. - -So what's the difference? Expressions return a value, and statements do not. -That’s why we end up with ‘not all control paths return a value’ here: the -statement `x + 1;` doesn’t return a value. There are two kinds of statements in -Rust: ‘declaration statements’ and ‘expression statements’. Everything else is -an expression. Let’s talk about declaration statements first. - -In some languages, variable bindings can be written as expressions, not just -statements. Like Ruby: + + + +これはRustについて2つの興味深いことを明らかにします。それが式ベースの言語であること、そしてセミコロンが他の「波括弧とセミコロン」ベースの言語でのセミコロンとは違っているということです。 +これら2つのことは関連します。 + + +## 式対文 + + + +Rustは主として式ベースの言語です。 +文には2種類しかなく、その他の全ては式です。 + + + + + + +ではその違いは何でしょうか。 +式は値を戻しますが、文は戻しません。 +それが「not all control paths return a value」で終わった理由です。文`x + 1;`は値を戻さないからです。 +Rustには2種類の文があります。「宣言文」と「式文」です。 +その他の全ては式です。 +まずは宣言文について話しましょう。 + + + +いくつかの言語では、変数束縛を文としてだけではなく、式として書くことができます。 +Rubyではこうなります。 ```ruby x = y = 5 ``` -In Rust, however, using `let` to introduce a binding is _not_ an expression. The -following will produce a compile-time error: + + +しかし、Rustでは束縛を導入するための`let`の使用は式では _ありません_ 。 +次の例はコンパイルエラーを起こします。 ```ignore let x = (let y = 5); // expected identifier, found keyword `let` ``` -The compiler is telling us here that it was expecting to see the beginning of -an expression, and a `let` can only begin a statement, not an expression. + + +ここでコンパイラは次のことを教えています。式の先頭を検出することが期待されていたところ、`let`は式ではなく文の先頭にしかなれないということです。 + + + + + + + +次のことに注意しましょう。既に束縛されている変数(例えば、`y = 5`)への割当ては、その値が特に役に立つものではなかったとしてもやはり式です。割当てが割り当てられる値(例えば、前の例では`5`)を評価する他の言語とは異なり、Rustでは割当ての値は空のタプル`()`です。なぜなら、割り当てられる値には[単一の所有者](ownership.html)しかおらず、他のどんな値を戻したとしても予想外の出来事になってしまうからです。 -Note that assigning to an already-bound variable (e.g. `y = 5`) is still an -expression, although its value is not particularly useful. Unlike other -languages where an assignment evaluates to the assigned value (e.g. `5` in the -previous example), in Rust the value of an assignment is an empty tuple `()` -because the assigned value can have [just one owner](ownership.html), and any -other returned value would be too surprising: ```rust let mut y = 5; @@ -155,16 +197,23 @@ let mut y = 5; let x = (y = 6); // x has the value `()`, not `6` ``` -The second kind of statement in Rust is the *expression statement*. Its -purpose is to turn any expression into a statement. In practical terms, Rust's -grammar expects statements to follow other statements. This means that you use -semicolons to separate expressions from each other. This means that Rust -looks a lot like most other languages that require you to use semicolons -at the end of every line, and you will see semicolons at the end of almost -every line of Rust code you see. - -What is this exception that makes us say "almost"? You saw it already, in this -code: + + + + + + + +Rustでの2種類目の文は *式文* です。 +これの目的は式を文に変換することです。 +実際にはRustの文法は文の後には他の文が続くことが期待されています。 +これはそれぞれの式を区切るためにセミコロンを使うということを意味します。 +これはRustが全ての行末にセミコロンを使うことを要求する他の言語のほとんどとよく似ていること、そして見られるRustのコードのほとんど全ての行末で、セミコロンが見られるということを意味します。 + + + +「ほとんど」と言ったところの例外は何でしょうか。 +この例で既に見ています。 ```rust fn add_one(x: i32) -> i32 { @@ -172,13 +221,18 @@ fn add_one(x: i32) -> i32 { } ``` -Our function claims to return an `i32`, but with a semicolon, it would return -`()` instead. Rust realizes this probably isn’t what we want, and suggests -removing the semicolon in the error we saw before. + + + +この関数は`i32`を戻そうとしていますが、セミコロンを付ければ、それは代わりに`()`を戻します。 +Rustはこの挙動がおそらく求めているものではないということを理解するので、前に見たエラーの中で、セミコロンを削除することを提案するのです。 -## Early returns + +## 早期のリターン -But what about early returns? Rust does have a keyword for that, `return`: + +しかし、早期のリターンについてはどうでしょうか。 +Rustはそのためのキーワード`return`を持っています。 ```rust fn foo(x: i32) -> i32 { @@ -189,8 +243,9 @@ fn foo(x: i32) -> i32 { } ``` -Using a `return` as the last line of a function works, but is considered poor -style: + + +`return`を関数の最後の行で使っても動きますが、それはよろしくないスタイルだと考えられています。 ```rust fn foo(x: i32) -> i32 { @@ -198,14 +253,17 @@ fn foo(x: i32) -> i32 { } ``` -The previous definition without `return` may look a bit strange if you haven’t -worked in an expression-based language before, but it becomes intuitive over -time. + + + +あなたがこれまで式ベースの言語を使ったことがなければ、`return`のない前の定義の方がちょっと変に見えるかもしれません。しかし、それは時間とともに直観的に感じられるようになります。 -## Diverging functions + +## ダイバージング関数 -Rust has some special syntax for ‘diverging functions’, which are functions that -do not return: + + +Rustはリターンしない関数、「ダイバージング関数」のための特別な構文をいくつか持っています。 ```rust fn diverges() -> ! { @@ -213,20 +271,25 @@ fn diverges() -> ! { } ``` -`panic!` is a macro, similar to `println!()` that we’ve already seen. Unlike -`println!()`, `panic!()` causes the current thread of execution to crash with -the given message. Because this function will cause a crash, it will never -return, and so it has the type ‘`!`’, which is read ‘diverges’. + + + + +`panic!`は既に見てきた`println!`と同様にマクロです。 +`println!`とは違って、`panic!`は実行中の現在のスレッドを与えられたメッセージとともにクラッシュさせます。 +この関数はクラッシュを引き起こすので、決してリターンしません。そのため、それは「ダイバージ」と読む、「`!`」型を持つのです。 -If you add a main function that calls `diverges()` and run it, you’ll get -some output that looks like this: + + +もし`diverges()`を呼び出すメイン関数を追加してそれを実行するならば、次のようなものが出力されるでしょう。 ```text thread ‘
’ panicked at ‘This function never returns!’, hello.rs:2 ``` -If you want more information, you can get a backtrace by setting the -`RUST_BACKTRACE` environment variable: + + +もしもっと情報を得たいと思うのであれば、`RUST_BACKTRACE`環境変数をセットすることでバックトレースを得ることができます。 ```text $ RUST_BACKTRACE=1 ./diverges @@ -247,7 +310,8 @@ stack backtrace: 13: 0x0 - ``` -`RUST_BACKTRACE` also works with Cargo’s `run` command: + +`RUST_BACKTRACE`はCargoの`run`コマンドでも使うことができます。 ```text $ RUST_BACKTRACE=1 cargo run @@ -269,7 +333,8 @@ stack backtrace: 13: 0x0 - ``` -A diverging function can be used as any type: + +ダイバージング関数は任意の型として使うことができます。 ```should_panic # fn diverges() -> ! { @@ -279,16 +344,20 @@ let x: i32 = diverges(); let x: String = diverges(); ``` -## Function pointers + +## 関数ポインタ -We can also create variable bindings which point to functions: + +関数を指示する変数束縛を作ることもできます。 ```rust let f: fn(i32) -> i32; ``` -`f` is a variable binding which points to a function that takes an `i32` as -an argument and returns an `i32`. For example: + + +`f`は`i32`を引数として受け取り、`i32`を戻す関数を指示する変数束縛です。 +例えばこうです。 ```rust fn plus_one(i: i32) -> i32 { @@ -302,7 +371,8 @@ let f: fn(i32) -> i32 = plus_one; let f = plus_one; ``` -We can then use `f` to call the function: + +そして、その関数を呼び出すために`f`を使うことができます。 ```rust # fn plus_one(i: i32) -> i32 { i + 1 } From 5f30c56c238d6c5751b3285b930290c4e653109b Mon Sep 17 00:00:00 2001 From: Yoshito Komatsu Date: Thu, 28 Jan 2016 07:18:01 +0000 Subject: [PATCH 3/7] Update functions --- 1.6/ja/book/functions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1.6/ja/book/functions.md b/1.6/ja/book/functions.md index 5b0eff8f..905e3dbf 100644 --- a/1.6/ja/book/functions.md +++ b/1.6/ja/book/functions.md @@ -228,10 +228,10 @@ fn add_one(x: i32) -> i32 { Rustはこの挙動がおそらく求めているものではないということを理解するので、前に見たエラーの中で、セミコロンを削除することを提案するのです。 -## 早期のリターン +## 途中でのリターン -しかし、早期のリターンについてはどうでしょうか。 +しかし、途中でのリターンについてはどうでしょうか。 Rustはそのためのキーワード`return`を持っています。 ```rust From a4330da0aaf3117f439b08ef8b10c82deccf058c Mon Sep 17 00:00:00 2001 From: Yoshito Komatsu Date: Thu, 28 Jan 2016 07:18:28 +0000 Subject: [PATCH 4/7] Update TranslationTable --- TranslationTable.md | 56 +++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/TranslationTable.md b/TranslationTable.md index 4cc13f13..5549d8f1 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -4,31 +4,37 @@ + 気持としては「無理矢理和訳」を避けたい。そのための基準。 + カタカナ語の方が用語として認識しやすい * カタカナ語の末尾の長音記号「ー」は省く(JIS規格) -* 構文キーワードなどはそののままアルファベットを使う +* 構文キーワードなどはそのままアルファベットを使う # 対訳表 -| English | 日本語 -|:-------------|:------ -| associated - | 関連- -| attribute | アトリビュート -| binding | 束縛 -| borrowing | 借用 -| coercion | 型強制 -| crate | クレート -| enum | 列挙型 -| `enum` | `enum` -| feature | フィーチャ -| Intrinsics | Intrinsic -| Lang Items | Lang Item -| lifetime | ライフタイム -| move | ムーブ -| `mut` | `mut` -| mutable | ミュータブル -| mutability | ミュータビリティ -| ownership | 所有権 -| struct | 構造体 -| `struct` | `struct` -| structure | ストラクチャ -| trait | トレイト -| unsized type | サイズ不定型 +| English | 日本語 +|:----------------------|:------ +| associated - | 関連- +| attribute | アトリビュート +| binding | 束縛 +| borrowing | 借用 +| coercion | 型強制 +| crate | クレート +| declaration statement | 宣言文 +| diverge | ダイバージ +| diverging | ダイバージング +| early return | 途中でのリターン +| enum | 列挙型 +| `enum` | `enum` +| expression statement | 式文 +| feature | フィーチャ +| Intrinsics | Intrinsic +| Lang Items | Lang Item +| lifetime | ライフタイム +| move | ムーブ +| `mut` | `mut` +| mutable | ミュータブル +| mutability | ミュータビリティ +| owner | 所有者 +| ownership | 所有権 +| struct | 構造体 +| `struct` | `struct` +| structure | ストラクチャ +| trait | トレイト +| unsized type | サイズ不定型 From 5d537ed22bbd82f921d6fe6b7a1c0c045da1cd91 Mon Sep 17 00:00:00 2001 From: Yoshito Komatsu Date: Fri, 29 Jan 2016 03:59:10 +0000 Subject: [PATCH 5/7] Fix words for 'return' --- 1.6/ja/book/functions.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/1.6/ja/book/functions.md b/1.6/ja/book/functions.md index 905e3dbf..e73b27da 100644 --- a/1.6/ja/book/functions.md +++ b/1.6/ja/book/functions.md @@ -110,7 +110,7 @@ fn add_one(x: i32) -> i32 { Rustの関数はちょうど1つだけの値を返します。そして、ダッシュ(`-`)の後ろに大なりの記号(`>`)を続けた「矢印」の後にその型を宣言します。 -関数の最後の行が何を戻すのかを決定します。 +関数の最後の行が何を返すのかを決定します。 ここにセミコロンがないことに気が付くでしょう。 もしそれを追加すると、こうなります。 @@ -154,8 +154,8 @@ Rustは主として式ベースの言語です。 ではその違いは何でしょうか。 -式は値を戻しますが、文は戻しません。 -それが「not all control paths return a value」で終わった理由です。文`x + 1;`は値を戻さないからです。 +式は値を返しますが、文は返しません。 +それが「not all control paths return a value」で終わった理由です。文`x + 1;`は値を返さないからです。 Rustには2種類の文があります。「宣言文」と「式文」です。 その他の全ては式です。 まずは宣言文について話しましょう。 @@ -188,7 +188,7 @@ let x = (let y = 5); // expected identifier, found keyword `let` -次のことに注意しましょう。既に束縛されている変数(例えば、`y = 5`)への割当ては、その値が特に役に立つものではなかったとしてもやはり式です。割当てが割り当てられる値(例えば、前の例では`5`)を評価する他の言語とは異なり、Rustでは割当ての値は空のタプル`()`です。なぜなら、割り当てられる値には[単一の所有者](ownership.html)しかおらず、他のどんな値を戻したとしても予想外の出来事になってしまうからです。 +次のことに注意しましょう。既に束縛されている変数(例えば、`y = 5`)への割当ては、その値が特に役に立つものではなかったとしてもやはり式です。割当てが割り当てられる値(例えば、前の例では`5`)を評価する他の言語とは異なり、Rustでは割当ての値は空のタプル`()`です。なぜなら、割り当てられる値には[単一の所有者](ownership.html)しかおらず、他のどんな値を返したとしても予想外の出来事になってしまうからです。 ```rust @@ -224,7 +224,7 @@ fn add_one(x: i32) -> i32 { -この関数は`i32`を戻そうとしていますが、セミコロンを付ければ、それは代わりに`()`を戻します。 +この関数は`i32`を返そうとしていますが、セミコロンを付ければ、それは代わりに`()`を返します。 Rustはこの挙動がおそらく求めているものではないということを理解するので、前に見たエラーの中で、セミコロンを削除することを提案するのです。 @@ -356,7 +356,7 @@ let f: fn(i32) -> i32; -`f`は`i32`を引数として受け取り、`i32`を戻す関数を指示する変数束縛です。 +`f`は`i32`を引数として受け取り、`i32`を返す関数を指示する変数束縛です。 例えばこうです。 ```rust From fe54850ec8c3520ca5d7157a97ceee995012ac12 Mon Sep 17 00:00:00 2001 From: Yoshito Komatsu Date: Fri, 29 Jan 2016 03:59:56 +0000 Subject: [PATCH 6/7] Update TranslationTable --- TranslationTable.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TranslationTable.md b/TranslationTable.md index 9620df53..b168babf 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -31,6 +31,7 @@ | mutability | ミュータビリティ | owner | 所有者 | ownership | 所有権 +| return | 返す | struct | 構造体 | structure | ストラクチャ | trait | トレイト From 8278668907ce21b15b879c4ca21327f2305d6309 Mon Sep 17 00:00:00 2001 From: Yoshito Komatsu Date: Fri, 29 Jan 2016 06:28:31 +0000 Subject: [PATCH 7/7] Update functions --- 1.6/ja/book/functions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1.6/ja/book/functions.md b/1.6/ja/book/functions.md index e73b27da..0da6d458 100644 --- a/1.6/ja/book/functions.md +++ b/1.6/ja/book/functions.md @@ -228,10 +228,10 @@ fn add_one(x: i32) -> i32 { Rustはこの挙動がおそらく求めているものではないということを理解するので、前に見たエラーの中で、セミコロンを削除することを提案するのです。 -## 途中でのリターン +## 早期リターン -しかし、途中でのリターンについてはどうでしょうか。 +しかし、早期リターンについてはどうでしょうか。 Rustはそのためのキーワード`return`を持っています。 ```rust