4
4
<!-- `struct`s are a way of creating more complex data types. For example, if we were
5
5
doing calculations involving coordinates in 2D space, we would need both an `x`
6
6
and a `y` value: -->
7
- ` struct ` はより複雑なデータ型を作る方法の1つです。例えば、もし私たちが2次元空間の座標に関する計算を行っているとして、` x ` と ` y ` 、両方の値が必要になるでしょう。
7
+ ` struct ` はより複雑なデータ型を作る方法の1つです。例えば、もし私たちが2次元空間の座標に関する計算を行っているとして、 ` x ` と ` y ` 、両方の値が必要になるでしょう。
8
8
9
9
``` rust
10
10
let origin_x = 0 ;
11
11
let origin_y = 0 ;
12
12
```
13
13
14
14
<!-- A `struct` lets us combine these two into a single, unified datatype: -->
15
- ` struct ` でこれら2つを1つのデータ型にまとめることができます。
15
+ ` struct ` でこれら2つを1つのデータ型にまとめることができます。
16
16
17
17
``` rust
18
18
struct Point {
@@ -30,20 +30,20 @@ fn main() {
30
30
<!-- There’s a lot going on here, so let’s break it down. We declare a `struct` with
31
31
the `struct` keyword, and then with a name. By convention, `struct`s begin with
32
32
a capital letter and are camel cased: `PointInSpace`, not `Point_In_Space`. -->
33
- ここで多くの情報が出てきましたから、順番に見ていきましょう。まず、` struct ` キーワードを使って構造体とその名前を宣言しています。慣習により、構造体は初めが大文字のキャメルケースで記述しています。` PointInSpace ` であり、` Point_In_Space ` ではありません。
33
+ ここで多くの情報が出てきましたから、順番に見ていきましょう。まず、 ` struct ` キーワードを使って構造体とその名前を宣言しています。慣習により、構造体は初めが大文字のキャメルケースで記述しています。 ` PointInSpace ` であり、 ` Point_In_Space ` ではありません。
34
34
35
35
<!-- We can create an instance of our `struct` via `let`, as usual, but we use a `key:
36
36
value` style syntax to set each field. The order doesn’t need to be the same as
37
37
in the original declaration. -->
38
- いつものように、` let ` で ` struct ` のインスタンスを作ることができますが、ここでは` key: value ` スタイルの構文でそれぞれのフィールドに値をセットしています。順序は元の宣言と同じである必要はありません。
38
+ いつものように、 ` let ` で ` struct ` のインスタンスを作ることができますが、ここでは ` key: value ` スタイルの構文でそれぞれのフィールドに値をセットしています。順序は元の宣言と同じである必要はありません。
39
39
40
40
<!-- Finally, because fields have names, we can access the field through dot
41
41
notation: `origin.x`. -->
42
- 最後に、作成された構造体のフィールドは名前を持つため、` origin.x ` というようにドット表記でアクセスできます。
42
+ 最後に、作成された構造体のフィールドは名前を持つため、 ` origin.x ` というようにドット表記でアクセスできます。
43
43
44
44
<!-- The values in `struct`s are immutable by default, like other bindings in Rust.
45
45
Use `mut` to make them mutable -->
46
- Rustの他の束縛のように、` struct ` が持つ値はイミュータブルがデフォルトです。` mut ` を使うと値をミュータブルにできます。
46
+ Rustの他の束縛のように、 ` struct ` が持つ値はイミュータブルがデフォルトです。 ` mut ` を使うと値をミュータブルにできます。
47
47
48
48
``` rust
49
49
struct Point {
@@ -61,7 +61,7 @@ fn main() {
61
61
```
62
62
63
63
<!-- This will print `The point is at (5, 0)`. -->
64
- これは` The point is at (5, 0) ` と出力されます。
64
+ これは ` The point is at (5, 0) ` と出力されます。
65
65
66
66
<!-- Rust does not support field mutability at the language level, so you cannot
67
67
write something like this: -->
@@ -105,7 +105,7 @@ fn main() {
105
105
106
106
<!-- A `struct` can include `..` to indicate that you want to use a copy of some
107
107
other `struct` for some of the values. For example: -->
108
- ` struct ` の初期化時には、値の一部を他の構造体からコピーしたいことを示す` .. ` を含めることができます。例えば、
108
+ ` struct ` の初期化時には、値の一部を他の構造体からコピーしたいことを示す ` .. ` を含めることができます。例えば、
109
109
110
110
``` rust
111
111
struct Point3d {
@@ -139,7 +139,7 @@ let point = Point3d { z: 1, x: 2, .. origin };
139
139
<!-- Rust has another data type that’s like a hybrid between a [tuple][tuple] and a
140
140
`struct`, called a ‘tuple struct’. Tuple structs have a name, but
141
141
their fields don’t:-->
142
- Rustには「タプル構造体」と呼ばれる、[ タプル] [ tuple ] と` struct ` のハイブリットのようなデータ型があります。タプル構造体自体には名前がありますが、そのフィールドには名前がありません。
142
+ Rustには「タプル構造体」と呼ばれる、[ タプル] [ tuple ] と ` struct ` のハイブリットのようなデータ型があります。タプル構造体自体には名前がありますが、そのフィールドには名前がありません。
143
143
144
144
``` rust
145
145
struct Color (i32 , i32 , i32 );
@@ -160,7 +160,7 @@ let origin = Point(0, 0, 0);
160
160
161
161
<!-- It is almost always better to use a `struct` than a tuple struct. We would write
162
162
`Color` and `Point` like this instead: -->
163
- ほとんどの場合タプル構造体よりも` struct ` を使ったほうが良いです。` Color ` や ` Point ` はこのようにも書けます。
163
+ ほとんどの場合タプル構造体よりも ` struct ` を使ったほうが良いです。 ` Color ` や ` Point ` はこのようにも書けます。
164
164
165
165
``` rust
166
166
struct Color {
@@ -178,7 +178,7 @@ struct Point {
178
178
179
179
<!-- Now, we have actual names, rather than positions. Good names are important,
180
180
and with a `struct`, we have actual names. -->
181
- 今、私たちはフィールドの位置ではなく実際のフィールドの名前を持っています。良い名前は重要で、` struct ` を使うということは、実際に名前を持っているということです。
181
+ 今、私たちはフィールドの位置ではなく実際のフィールドの名前を持っています。良い名前は重要で、 ` struct ` を使うということは、実際に名前を持っているということです。
182
182
183
183
> 訳注: 原文を元に噛み砕くと、「タプルはフィールドの並びによって区別され、構造体はフィールドの名前によって区別されます。これはタプルと構造体の最たる違いであり、構造体を持つことは名前を付けられたデータの集まりを持つことに等しいため、構造体における名前付けは重要です。」といった所でしょうか。
184
184
@@ -200,13 +200,13 @@ println!("length is {} inches", integer_length);
200
200
<!-- As you can see here, you can extract the inner integer type through a
201
201
destructuring `let`, just as with regular tuples. In this case, the
202
202
`let Inches(integer_length)` assigns `10` to `integer_length`. -->
203
- 上記の通り、` let ` を使って分解することで、標準のタプルと同じように内部の整数型を取り出すことができます。
204
- このケースでは` let Inches(integer_length) ` が ` integer_length ` へ ` 10 ` を束縛します。
203
+ 上記の通り、 ` let ` を使って分解することで、標準のタプルと同じように内部の整数型を取り出すことができます。
204
+ このケースでは ` let Inches(integer_length) ` が ` integer_length ` へ ` 10 ` を束縛します。
205
205
206
206
# Unit-like 構造体
207
207
208
208
<!-- You can define a `struct` with no members at all: -->
209
- あなたは全くメンバを持たない` struct ` を定義できます。
209
+ あなたは全くメンバを持たない ` struct ` を定義できます。
210
210
211
211
``` rust
212
212
struct Electron ;
@@ -217,14 +217,14 @@ let x = Electron;
217
217
<!-- Such a `struct` is called ‘unit-like’ because it resembles the empty
218
218
tuple, `()`, sometimes called ‘unit’. Like a tuple struct, it defines a
219
219
new type. -->
220
- 空のタプルである` () ` は時々` unit ` と呼ばれ、それに似ていることからこのような構造体を` unit-like ` と呼んでいます。タプル構造体のように、これは新しい型を定義します。
220
+ 空のタプルである ` () ` は時々 ` unit ` と呼ばれ、それに似ていることからこのような構造体を ` unit-like ` と呼んでいます。タプル構造体のように、これは新しい型を定義します。
221
221
222
222
<!-- This is rarely useful on its own (although sometimes it can serve as a
223
223
marker type), but in combination with other features, it can become
224
224
useful. For instance, a library may ask you to create a structure that
225
225
implements a certain [trait][trait] to handle events. If you don’t have
226
226
any data you need to store in the structure, you can just create a
227
227
unit-like `struct`. -->
228
- これは単体でもごくまれに役立ちます(もっとも、時々型をマーク代わりとして役立てる程度です)が、他の機能と組み合わせることにより便利になります。例えば、ライブラリはあなたにイベントを処理できる特定の[ トレイト] [ trait ] が実装されたストラクチャの作成を要求するかもしれません。もしそのストラクチャの中に保存すべき値が何もなければ、あなたはダミーのデータを作成する必要はなく、ただunit-likeな` struct ` を作るだけで良いのです。
228
+ これは単体でもごくまれに役立ちます(もっとも、時々型をマーク代わりとして役立てる程度です)が、他の機能と組み合わせることにより便利になります。例えば、ライブラリはあなたにイベントを処理できる特定の[ トレイト] [ trait ] が実装されたストラクチャの作成を要求するかもしれません。もしそのストラクチャの中に保存すべき値が何もなければ、あなたはダミーのデータを作成する必要はなく、ただunit-likeな ` struct ` を作るだけで良いのです。
229
229
230
230
[ trait ] : traits.html
0 commit comments