diff --git a/1.6/ja/book/structs.md b/1.6/ja/book/structs.md index b51ad8e0..c6fbb8a5 100644 --- a/1.6/ja/book/structs.md +++ b/1.6/ja/book/structs.md @@ -1,15 +1,18 @@ -% Structs +% 構造体 + -`struct`s are a way of creating more complex data types. For example, if we were + +`struct`はより複雑なデータ型を作る方法の1つです。例えば、もし私たちが2次元空間の座標に関する計算を行っているとして、`x`と`y`、両方の値が必要になるでしょう。 ```rust let origin_x = 0; let origin_y = 0; ``` -A `struct` lets us combine these two into a single, unified datatype: + +`struct`でこれら2つを1つのデータ型にまとめることができます。 ```rust struct Point { @@ -24,20 +27,23 @@ fn main() { } ``` -There’s a lot going on here, so let’s break it down. We declare a `struct` with + +ここで多くの情報が出てきましたから、順番に見ていきましょう。まず、`struct`キーワードを使って構造体とその名前を宣言しています。慣習により、構造体は初めが大文字のキャメルケースで記述しています。`PointInSpace`であり、`Point_In_Space`ではありません。 -We can create an instance of our `struct` via `let`, as usual, but we use a `key: + +いつものように、`let`で`struct`のインスタンスを作ることができますが、ここでは`key: value`スタイルの構文でそれぞれのフィールドに値をセットしています。順序は元の宣言と同じである必要はありません。 -Finally, because fields have names, we can access the field through dot -notation: `origin.x`. - -The values in `struct`s are immutable by default, like other bindings in Rust. -Use `mut` to make them mutable: + +最後に、作成された構造体のフィールドは名前を持つため、`origin.x`というようにドット表記でアクセスできます。 + +Rustの他の束縛のように、`struct`が持つ値はイミュータブルがデフォルトです。`mut`を使うと値をミュータブルにできます。 ```rust struct Point { x: i32, @@ -53,10 +59,13 @@ fn main() { } ``` -This will print `The point is at (5, 0)`. + +これは`The point is at (5, 0)`と出力されます。 + + +Rustは言語レベルでフィールドのミュータビリティに対応していないため、以下の様に書くことはできません。 -Rust does not support field mutability at the language level, so you cannot -write something like this: ```rust,ignore struct Point { @@ -65,11 +74,27 @@ struct Point { } ``` -Mutability is a property of the binding, not of the structure itself. If you’re + +ミュータビリティは束縛に付与できる属性であり、構造体自体に付与できる属性ではありません。もしあなたがフィールドレベルのミュータビリティを使うのであれば、初めこそ奇妙に見えるものの、非常に簡単に実現できる方法があります。以下の方法で少しの間だけミュータブルな構造体を作ることができます。 + ```rust,ignore struct Point { @@ -82,17 +107,18 @@ fn main() { point.x = 5; - let point = point; // this new binding can’t change now + let point = point; // この新しい束縛でここから変更できなくなります - point.y = 6; // this causes an error + point.y = 6; // これはエラーになります } ``` -# Update syntax - -A `struct` can include `..` to indicate that you want to use a copy of some -other `struct` for some of the values. For example: + +# アップデート構文 + +`struct`の初期化時には、値の一部を他の構造体からコピーしたいことを示す`..`を含めることができます。例えば、 ```rust struct Point3d { x: i32, @@ -104,9 +130,10 @@ let mut point = Point3d { x: 0, y: 0, z: 0 }; point = Point3d { y: 1, .. point }; ``` -This gives `point` a new `y`, but keeps the old `x` and `z` values. It doesn’t + +ここでは`point`に新しい`y`を与えていますが、`x`と`z`は古い値を維持します。どれかの`struct`と同じ値を作る他にも、この構文を新たな値の作成に使用でき、明示することなく値のコピーが行えます。 ```rust # struct Point3d { @@ -118,11 +145,13 @@ let origin = Point3d { x: 0, y: 0, z: 0 }; let point = Point3d { z: 1, x: 2, .. origin }; ``` -# Tuple structs + +# タプル構造体 -Rust has another data type that’s like a hybrid between a [tuple][tuple] and a + +Rustには「タプル構造体」と呼ばれる、[タプル][tuple]と`struct`のハイブリットのようなデータ型があります。タプル構造体自体には名前がありますが、そのフィールドには名前がありません。 ```rust struct Color(i32, i32, i32); @@ -131,7 +160,8 @@ struct Point(i32, i32, i32); [tuple]: primitive-types.html#tuples -These two will not be equal, even if they have the same values: + +これら2つは同じ値を持つ同士であったとしても等しくありません。 ```rust # struct Color(i32, i32, i32); @@ -140,8 +170,9 @@ let black = Color(0, 0, 0); let origin = Point(0, 0, 0); ``` -It is almost always better to use a `struct` than a tuple struct. We would write -`Color` and `Point` like this instead: + +ほとんどの場合タプル構造体よりも`struct`を使ったほうが良いです。`Color`や`Point`はこのようにも書けます。 ```rust struct Color { @@ -157,13 +188,17 @@ struct Point { } ``` -Now, we have actual names, rather than positions. Good names are important, -and with a `struct`, we have actual names. + +今、私たちはフィールドの位置ではなく実際のフィールドの名前を持っています。良い名前は重要で、`struct`を使うということは、実際に名前を持っているということです。 -There _is_ one case when a tuple struct is very useful, though, and that’s a +> 訳注: 原文を元に噛み砕くと、「タプルはフィールドの並びによって区別され、構造体はフィールドの名前によって区別されます。これはタプルと構造体の最たる違いであり、構造体を持つことは名前を付けられたデータの集まりを持つことに等しいため、構造体における名前付けは重要です。」といった所でしょうか。 + + +タプル構造体が非常に便利な場合も_あります_が、1要素で使う場合だけです。タプル構造体の中に入っている値と、それ自体のセマンティックな表現を明確に区別できるような新しい型を作成できることから、私たちはこれを「newtype」パターンと呼んでいます。 ```rust struct Inches(i32); @@ -174,29 +209,33 @@ let Inches(integer_length) = length; println!("length is {} inches", integer_length); ``` -As you can see here, you can extract the inner integer type through a + +上記の通り、`let`を使って分解することで、標準のタプルと同じように内部の整数型を取り出すことができます。 +このケースでは`let Inches(integer_length)`が`integer_length`へ`10`を束縛します。 -You can define a `struct` with no members at all: +# Unit-like 構造体 + +あなたは全くメンバを持たない`struct`を定義できます。 ```rust struct Electron; let x = Electron; ``` -Such a `struct` is called ‘unit-like’ because it resembles the empty + +空のタプルである`()`は時々`unit`と呼ばれ、それに似ていることからこのような構造体を`unit-like`と呼んでいます。タプル構造体のように、これは新しい型を定義します。 -This is rarely useful on its own (although sometimes it can serve as a + +これは単体でもごくまれに役立ちます(もっとも、時々型をマーク代わりとして役立てる程度です)が、他の機能と組み合わせることにより便利になります。例えば、ライブラリはあなたにイベントを処理できる特定の[トレイト][trait]が実装されたストラクチャの作成を要求するかもしれません。もしそのストラクチャの中に保存すべき値が何もなければ、あなたはダミーのデータを作成する必要はなく、ただunit-likeな`struct`を作るだけで良いのです。 [trait]: traits.html