diff --git a/1.6/ja/book/guessing-game.md b/1.6/ja/book/guessing-game.md index 50147480..7c52e2ce 100644 --- a/1.6/ja/book/guessing-game.md +++ b/1.6/ja/book/guessing-game.md @@ -917,9 +917,9 @@ fn main() { ちょっと待って下さい、既に`guess`を定義してありますよね? -してあります、が、Rustでは以前の`guess`の定義を新しいもので「隠す」ことが出来ます(訳注: このように隠すことをシャドイングといいます)。 +してあります、が、Rustでは以前の`guess`の定義を新しいもので「隠す」ことが出来ます(訳注: このように隠すことをシャドーイングといいます)。 まさにこのように、最初`String`であった`guess`を`u32`に変換したい、というような状況でよく使われます。 -シャドイングのおかげで`guess_str`と`guess`のように別々の名前を考える必要はなくなり、`guess`の名前を再利用出来ます。 +シャドーイングのおかげで`guess_str`と`guess`のように別々の名前を考える必要はなくなり、`guess`の名前を再利用出来ます。 `guess`を先に書いたような値に束縛します。 diff --git a/1.6/ja/book/variable-bindings.md b/1.6/ja/book/variable-bindings.md index f3a5d1dd..832bc059 100644 --- a/1.6/ja/book/variable-bindings.md +++ b/1.6/ja/book/variable-bindings.md @@ -1,8 +1,15 @@ -% Variable Bindings +% 変数束縛 + -Virtually every non-'Hello World’ Rust program uses *variable bindings*. They -bind some value to a name, so it can be used later. `let` is -used to introduce a binding, just like this: + + + +事実上全ての「Hello World」でないRustのプログラムは *変数束縛* を使っています。 +変数束縛は何らかの値を名前へと束縛するので、後でその値を使えます。 +このように、 `let` が束縛を導入するのに使われています。 + +> 訳注: 普通、束縛というときは名前 *を* 値 *へ* と束縛しますが、このドキュメントでは逆になっています。 +> Rustでは他の言語と違って1つの値に対して1つの名前が対応するのであえてこう書いてるのかもしれません。 ```rust fn main() { @@ -10,52 +17,72 @@ fn main() { } ``` -Putting `fn main() {` in each example is a bit tedious, so we’ll leave that out -in the future. If you’re following along, make sure to edit your `main()` -function, rather than leaving it off. Otherwise, you’ll get an error. + + + +例で毎回 `fn main() {` と書くのは長ったらしいのでこれ以後は省略します。 +もし試しながら読んでいるのならそのまま書くのではなくちゃんと `main()` 関数の中身を編集するようにしてください。そうしないとエラーになります。 -# Patterns + +# パターン -In many languages, a variable binding would be called a *variable*, but Rust’s -variable bindings have a few tricks up their sleeves. For example the -left-hand side of a `let` expression is a ‘[pattern][pattern]’, not just a -variable name. This means we can do things like: + + + + +多くの言語では変数束縛は *変数* と呼ばれるでしょうが、Rustの変数束縛は多少皮を被せてあります。 +例えば、 `let` の左側の式は「[パターン][pattern]」であって、ただの変数名ではありません。 +これはこのようなことが出来るということです。 ```rust let (x, y) = (1, 2); ``` -After this expression is evaluated, `x` will be one, and `y` will be two. -Patterns are really powerful, and have [their own section][pattern] in the -book. We don’t need those features for now, so we’ll just keep this in the back -of our minds as we go forward. + + + + +パターン式が評価されたあと、 `x` は1になり、 `y` は2になります。 +パターンは本当に強力で、本書には[パターンのセクション][pattern]もあります。 +今のところこの機能は必要ないので頭の片隅に留めておいてだけいて下さい。 [pattern]: patterns.html -# Type annotations + +# 型アノテーション -Rust is a statically typed language, which means that we specify our types up -front, and they’re checked at compile time. So why does our first example -compile? Well, Rust has this thing called ‘type inference’. If it can figure -out what the type of something is, Rust doesn’t require you to actually type it -out. + + + + + +Rustは静的な型付言語であり、前もって型を与えておいて、それがコンパイル時に検査されます。 +じゃあなぜ最初の例はコンパイルが通るのでしょう?ええと、Rustには「型推論」と呼ばれるものがあります。 +型推論が型が何であるか判断出来るなら、型を書く必要はなくなります。 -We can add the type if we want to, though. Types come after a colon (`:`): + +書きたいなら型を書くことも出来ます。型はコロン(`:`)のあとに書きます。 ```rust let x: i32 = 5; ``` -If I asked you to read this out loud to the rest of the class, you’d say “`x` -is a binding with the type `i32` and the value `five`.” + + +これをクラスのみんなに聞こえるように声に出して読むなら、「 `x` は型 `i32` を持つ束縛で、値は `五` である。」となります。 -In this case we chose to represent `x` as a 32-bit signed integer. Rust has -many different primitive integer types. They begin with `i` for signed integers -and `u` for unsigned integers. The possible integer sizes are 8, 16, 32, and 64 -bits. + + + + +この場合 `x` を32bit符号付き整数として表現することを選びました。 +Rustには多くのプリミティブな整数型があります。プリミティブな整数型は符号付き型は `i` 、符号無し型は `u` から始まります。 +整数型として可能なサイズは8、16、32、64ビットです。 -In future examples, we may annotate the type in a comment. The examples will -look like this: + + +以後の例では型はコメントで注釈することにします。 +先の例はこのようになります。 ```rust fn main() { @@ -63,21 +90,25 @@ fn main() { } ``` -Note the similarities between this annotation and the syntax you use with -`let`. Including these kinds of comments is not idiomatic Rust, but we'll -occasionally include them to help you understand what the types that Rust -infers are. - -# Mutability + + + + +この注釈と `let` の時に使う記法の類似性に留意して下さい。 +このようなコメントを書くのはRust的ではありませんが、時折理解の手助けのためにRustが推論する型をコメントで注釈します。 -By default, bindings are *immutable*. This code will not compile: + +# 可変性 + +デフォルトで、 束縛は *イミュータブル* です。このコードのコンパイルは通りません。 ```rust,ignore let x = 5; x = 10; ``` -It will give you this error: + +次のようなエラーが出ます。 ```text error: re-assignment of immutable variable `x` @@ -85,32 +116,49 @@ error: re-assignment of immutable variable `x` ^~~~~~~ ``` -If you want a binding to be mutable, you can use `mut`: +> 訳注: +> ``` +> エラー: イミュータブルな変数 `x` に再代入しています +> ``` + + + +束縛をミュータブルにしたいなら、`mut`が使えます。 ```rust let mut x = 5; // mut x: i32 x = 10; ``` -There is no single reason that bindings are immutable by default, but we can -think about it through one of Rust’s primary focuses: safety. If you forget to -say `mut`, the compiler will catch it, and let you know that you have mutated -something you may not have intended to mutate. If bindings were mutable by -default, the compiler would not be able to tell you this. If you _did_ intend -mutation, then the solution is quite easy: add `mut`. - -There are other good reasons to avoid mutable state when possible, but they’re -out of the scope of this guide. In general, you can often avoid explicit -mutation, and so it is preferable in Rust. That said, sometimes, mutation is -what you need, so it’s not verboten. - -# Initializing bindings - -Rust variable bindings have one more aspect that differs from other languages: -bindings are required to be initialized with a value before you're allowed to -use them. - -Let’s try it out. Change your `src/main.rs` file to look like this: + + + + + + +束縛がデフォルトでイミュータブルであるのは複合的な理由によるものですが、Rustの主要な焦点、安全性の一環だと考えることが出来ます。 +もし `mut` を忘れたらコンパイラが捕捉して、変更するつもりでなかったものを変更した旨を教えてくれます。 +束縛がデフォルトでミュータブルだったらコンパイラはこれを捕捉できません。 +もし _本当に_ 変更を意図していたのなら話は簡単です。 `mut` をつけ加えればいいのです。 + + + + + +可能な時はにミュータブルを避けた方が良い理由は他にもあるのですがそれはこのガイドの範囲を越えています。 +一般に、明示的な変更は避けられることが多いのでRustでもそうした方が良いのです。 +しかし変更が本当に必要なこともあるという意味で、厳禁という訳ではないのです。 + + +# 束縛を初期化する + + + + +Rustの束縛はもう1つ他の言語と異る点があります。束縛を使う前に値で初期化されている必要があるのです。 + + +試してみましょう。 `src/main.rs` をいじってこのようにしてみて下さい。 ```rust fn main() { @@ -120,8 +168,10 @@ fn main() { } ``` -You can use `cargo build` on the command line to build it. You’ll get a -warning, but it will still print "Hello, world!": + + +コマンドラインで `cargo build` を使ってビルド出来ます。 +警告が出ますが、それでもまだ「Hello, world!」は印字されます。 ```text Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world) @@ -131,9 +181,12 @@ src/main.rs:2 let x: i32; ^ ``` -Rust warns us that we never use the variable binding, but since we never use -it, no harm, no foul. Things change if we try to actually use this `x`, -however. Let’s do that. Change your program to look like this: + + + +Rustは一度も使われない変数について警告を出しますが、一度も使われないので人畜無害です。 +ところがこの `x` を使おうとすると事は一変します。やってみましょう。 +プログラムをこのように変更して下さい。 ```rust,ignore fn main() { @@ -143,7 +196,8 @@ fn main() { } ``` -And try to build it. You’ll get an error: + +そしてビルドしてみて下さい。このようなエラーが出る筈です。 ```bash $ cargo build @@ -159,32 +213,48 @@ error: aborting due to previous error Could not compile `hello_world`. ``` -Rust will not let us use a value that has not been initialized. Next, let’s -talk about this stuff we've added to `println!`. - -If you include two curly braces (`{}`, some call them moustaches...) in your -string to print, Rust will interpret this as a request to interpolate some sort -of value. *String interpolation* is a computer science term that means "stick -in the middle of a string." We add a comma, and then `x`, to indicate that we -want `x` to be the value we’re interpolating. The comma is used to separate -arguments we pass to functions and macros, if you’re passing more than one. - -When you just use the curly braces, Rust will attempt to display the value in a -meaningful way by checking out its type. If you want to specify the format in a -more detailed manner, there are a [wide number of options available][format]. -For now, we'll just stick to the default: integers aren't very complicated to -print. + + +Rustでは未初期化の値を使うことは許されていません。 +次に、 `println!` に追加したものについて話しましょう。 + + + + + + + +印字する文字列に2つの波括弧(`{}`、口髭という人もいます…(訳注: 海外の顔文字は横になっているので首を傾けて `{` を眺めてみて下さい。また、日本語だと「中括弧」と呼ぶ人もいますね))を入れました。 +Rustはこれを何かの値を入れて(interpolate、インターポーレート)くれという要求だと解釈します。 +*文字列インターポーレーション* (String interpolation)はコンピュータサイエンスの用語で、「文字列の中に差し込む」という意味です。 +その後に続けてカンマ、そして `x` を置いて `x` がインターポーレートしようとしている値だと指示しています。 +カンマは2つ以上の引数を関数やマクロに渡す時に使われます。 + + + + + + +単に波括弧だけを使った時は、Rustはインターポーレートされる値の型を調べて意味のある方法で表示しようとします。 +フォーマットをさらに詳しく指定したいなら[数多くのオプションが利用出来ます][format]。 +とりあえずのところ、デフォルトに従ましょう。整数の印字はそれほど複雑ではありません。 [format]: ../std/fmt/index.html -# Scope and shadowing + +# スコープとシャドーイング + + + + + + + +束縛に話を戻しましょう。変数束縛にはスコープがあります。変数束縛は定義されたブロック内でしか有効でありません。 +ブロックは `{` と `}` に囲まれた文の集まりです。関数定義もブロックです! +以下の例では異なるブロックで有効な2つの変数束縛、 `x` と `y` を定義しています。 +`x` は `fn main() {}` ブロックの中でアクセス可能ですが、 `y` は内側のブロックからのみアクセス出来ます。 -Let’s get back to bindings. Variable bindings have a scope - they are -constrained to live in a block they were defined in. A block is a collection -of statements enclosed by `{` and `}`. Function definitions are also blocks! -In the following example we define two variable bindings, `x` and `y`, which -live in different blocks. `x` can be accessed from inside the `fn main() {}` -block, while `y` can be accessed only from inside the inner block: ```rust,ignore fn main() { @@ -193,20 +263,24 @@ fn main() { let y: i32 = 3; println!("The value of x is {} and value of y is {}", x, y); } - println!("The value of x is {} and value of y is {}", x, y); // This won't work +# // println!("The value of x is {} and value of y is {}", x, y); // This won't work + println!("The value of x is {} and value of y is {}", x, y); // これは動きません } ``` -The first `println!` would print "The value of x is 17 and the value of y is -3", but this example cannot be compiled successfully, because the second -`println!` cannot access the value of `y`, since it is not in scope anymore. -Instead we get this error: + + + + +最初の `println!` は「The value of x is 17 and the value of y is 3」(訳注: 「xの値は17でyの値は3」)と印字する筈ですが、 +2つめの `println!` は `y` がもうスコープにいないため `y` にアクセス出来ないのでこの例はコンパイル出来ません。 +代わりに以下のようなエラーが出ます。 ```bash $ cargo build Compiling hello v0.1.0 (file:///home/you/projects/hello_world) main.rs:7:62: 7:63 error: unresolved name `y`. Did you mean `x`? [E0425] -main.rs:7 println!("The value of x is {} and value of y is {}", x, y); // This won't work +main.rs:7 println!("The value of x is {} and value of y is {}", x, y); // これは動きません ^ note: in expansion of format_args! :2:25: 2:56 note: expansion site @@ -221,32 +295,42 @@ Could not compile `hello`. To learn more, run the command again with --verbose. ``` -Additionally, variable bindings can be shadowed. This means that a later -variable binding with the same name as another binding, that's currently in -scope, will override the previous binding. + + + +さらに加えて、変数束縛は覆い隠すことが出来ます(訳注: このことをシャドーイングと言います)。 +つまり後に出てくる同じ名前の変数束縛があるとそれがスコープに入り、以前の束縛を上書きするのです。 ```rust let x: i32 = 8; { - println!("{}", x); // Prints "8" +# // println!("{}", x); // Prints "8" + println!("{}", x); // "8"を印字する let x = 12; - println!("{}", x); // Prints "12" +# // println!("{}", x); // Prints "12" + println!("{}", x); // "12"を印字する } -println!("{}", x); // Prints "8" +# // println!("{}", x); // Prints "8" +println!("{}", x); // "8"を印字する let x = 42; -println!("{}", x); // Prints "42" +# // println!("{}", x); // Prints "42" +println!("{}", x); // "42"を印字する ``` -Shadowing and mutable bindings may appear as two sides of the same coin, but -they are two distinct concepts that can't always be used interchangeably. For -one, shadowing enables us to rebind a name to a value of a different type. It -is also possible to change the mutability of a binding. + + + + +シャドーイングとミュータブルな束縛はコインの表と裏のように見えるかもしれませんが、それぞれ独立な概念であり互いに代用が出来ないケースがあります。 +その1つにシャドーイングは同じ名前に違う型の値を再束縛することが出来ます。 ```rust let mut x: i32 = 1; x = 7; -let x = x; // x is now immutable and is bound to 7 +# // let x = x; // x is now immutable and is bound to 7 +let x = x; // xはイミュータブルになって7に束縛されました let y = 4; -let y = "I can also be bound to text!"; // y is now of a different type +# // let y = "I can also be bound to text!"; // y is now of a different type +let y = "I can also be bound to text!"; // yは違う型になりました ``` diff --git a/TranslationTable.md b/TranslationTable.md index 668b88e3..9dd8008f 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -16,6 +16,7 @@ | application | アプリケーション | arity | アリティ | array | 配列 +| assignment | 代入 | associated - | 関連- | attribute | アトリビュート | binary | バイナリ @@ -26,6 +27,7 @@ | capture | キャプチャ | closure | クロージャ | coercion | 型強制 +| comma | カンマ | compiler | コンパイラ | constant | 定数 | crate | クレート @@ -45,6 +47,9 @@ | generics | ジェネリクス | identifier | 識別子 | immutable | イミュータブル +| initialize | 初期化する +| interpolate | インターポーレートする +| interpolation | インターポーレーション | keyword | キーワード | Intrinsics | Intrinsic | Lang Items | Lang Item @@ -58,6 +63,7 @@ | move | ムーブ | mutable | ミュータブル | mutability | ミュータビリティ +| mutable binding | ミュータブルな束縛 | owner | 所有者 | ownership | 所有権 | panic | パニック @@ -65,11 +71,18 @@ | pattern | パターン | performance | パフォーマンス | pointer | ポインタ +| re-assignment | 再代入 +| rebind | 再束縛 | return | 返す +| scope | スコープ | shadow | 覆い隠す +| shadowing | シャドーイング | signature | シグネチャ +| signed | 符号付き | slice | スライス | slicing | スライシング +| string | 文字列 +| string interpolation | 文字列インターポーレーション | struct | 構造体 | structure | ストラクチャ | symbol | シンボル @@ -77,7 +90,12 @@ | system | システム | tick | クオート | trait | トレイト +| type inference | 型推論 | Universal Function Call Syntax | 共通の関数呼び出し構文 +| unsigned | 符号無し | unsized type | サイズ不定型 +| variable | 変数 +| variable binding | 変数束縛 | vector | ベクタ +| warning | ウォーニング | wildcard | ワイルドカード