4
4
< meta charset ="utf-8 ">
5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
6
< meta name ="generator " content ="rustdoc ">
7
- < title > `type` Aliases </ title >
7
+ < title > `type` エイリアス </ title >
8
8
9
9
< link rel ="stylesheet " type ="text/css " href ="rustbook.css ">
10
10
185
185
< div id ='page '>
186
186
187
187
188
- < h1 class ="title "> `type` Aliases</ h1 >
189
- < p > The < code > type</ code > keyword lets you declare an alias of another type:</ p >
188
+ < h1 class ="title "> `type` エイリアス</ h1 >
189
+ <!-- % `type` Aliases -->
190
+
191
+ <!-- The `type` keyword lets you declare an alias of another type: -->
192
+
193
+ < p > < code > type</ code > キーワードを用いることで他の型へのエイリアスを宣言することができます:</ p >
190
194
< span class ='rusttest '> fn main() {
191
195
type Name = String;
192
196
}</ span > < pre class ='rust rust-example-rendered '>
193
197
< span class ='kw '> type</ span > < span class ='ident '> Name</ span > < span class ='op '> =</ span > < span class ='ident '> String</ span > ;</ pre >
194
198
195
- < p > You can then use this type as if it were a real type:</ p >
199
+ <!-- You can then use this type as if it were a real type: -->
200
+
201
+ < p > このようにすると 定義した型を実際の型であるかのように利用することができます:</ p >
196
202
< span class ='rusttest '> fn main() {
197
203
type Name = String;
198
204
@@ -202,9 +208,15 @@ <h1 class="title">`type` Aliases</h1>
202
208
203
209
< span class ='kw '> let</ span > < span class ='ident '> x</ span > : < span class ='ident '> Name</ span > < span class ='op '> =</ span > < span class ='string '> "Hello"</ span > .< span class ='ident '> to_string</ span > ();</ pre >
204
210
205
- < p > Note, however, that this is an < em > alias</ em > , not a new type entirely. In other
206
- words, because Rust is strongly typed, you’d expect a comparison between two
207
- different types to fail:</ p >
211
+ <!-- Note, however, that this is an _alias_, not a new type entirely. In other -->
212
+
213
+ <!-- words, because Rust is strongly typed, you’d expect a comparison between two -->
214
+
215
+ <!-- different types to fail: -->
216
+
217
+ < p > しかしながら、これはあくまで < em > エイリアス</ em > であって、新しい型ではありません。
218
+ 言い換えると、Rustは強い型付け言語であるため、異なる型同士の比較が失敗することを期待するでしょう。
219
+ 例えば:</ p >
208
220
< span class ='rusttest '> fn main() {
209
221
let x: i32 = 5;
210
222
let y: i64 = 5;
@@ -220,7 +232,9 @@ <h1 class="title">`type` Aliases</h1>
220
232
< span class ='comment '> // ...</ span >
221
233
}</ pre >
222
234
223
- < p > this gives</ p >
235
+ <!-- this gives -->
236
+
237
+ < p > このようなコードは以下のエラーを発生させます:</ p >
224
238
225
239
< pre > < code class ="language-text "> error: mismatched types:
226
240
expected `i32`,
@@ -231,7 +245,9 @@ <h1 class="title">`type` Aliases</h1>
231
245
^
232
246
</ code > </ pre >
233
247
234
- < p > But, if we had an alias:</ p >
248
+ <!-- But, if we had an alias: -->
249
+
250
+ < p > 一方で、エイリアス用いた場合は:</ p >
235
251
< span class ='rusttest '> fn main() {
236
252
type Num = i32;
237
253
@@ -251,10 +267,16 @@ <h1 class="title">`type` Aliases</h1>
251
267
< span class ='comment '> // ...</ span >
252
268
}</ pre >
253
269
254
- < p > This compiles without error. Values of a < code > Num</ code > type are the same as a value of
255
- type < code > i32</ code > , in every way. You can use < a href ="structs.html#tuple-structs "> tuple struct</ a > to really get a new type.</ p >
270
+ <!-- This compiles without error. Values of a `Num` type are the same as a value of -->
256
271
257
- < p > You can also use type aliases with generics:</ p >
272
+ <!-- type `i32`, in every way. You can use [tuple struct] to really get a new type. -->
273
+
274
+ < p > このコードはエラーを起こすこと無くコンパイルを通ります。 < code > Num</ code > 型の値は < code > i32</ code > 型の値とすべての面において等価です。
275
+ 本当に新しい型がほしい時は < a href ="structs.html#tuple-structs "> タプル構造体</ a > を使うことができます。</ p >
276
+
277
+ <!-- You can also use type aliases with generics: -->
278
+
279
+ < p > また、エイリアスをジェネリクスと共に利用する事もできます:</ p >
258
280
< span class ='rusttest '> fn main() {
259
281
use std::result;
260
282
@@ -274,10 +296,17 @@ <h1 class="title">`type` Aliases</h1>
274
296
275
297
< span class ='kw '> type</ span > < span class ='prelude-ty '> Result</ span > < span class ='op '> <</ span > < span class ='ident '> T</ span > < span class ='op '> ></ span > < span class ='op '> =</ span > < span class ='ident '> result</ span > ::< span class ='prelude-ty '> Result</ span > < span class ='op '> <</ span > < span class ='ident '> T</ span > , < span class ='ident '> ConcreteError</ span > < span class ='op '> ></ span > ;</ pre >
276
298
277
- < p > This creates a specialized version of the < code > Result</ code > type, which always has a
278
- < code > ConcreteError</ code > for the < code > E</ code > part of < code > Result<T, E></ code > . This is commonly used
279
- in the standard library to create custom errors for each subsection. For
280
- example, < a href ="../std/io/type.Result.html "> io::Result</ a > .</ p >
299
+ <!-- This creates a specialized version of the `Result` type, which always has a -->
300
+
301
+ <!-- `ConcreteError` for the `E` part of `Result<T, E>`. This is commonly used -->
302
+
303
+ <!-- in the standard library to create custom errors for each subsection. For -->
304
+
305
+ <!-- example, [io::Result][ioresult]. -->
306
+
307
+ < p > このようにすると < code > Result</ code > 型の < code > Result<T, E></ code > の < code > E</ code > として常に < code > ConcreteError</ code > を持っている特殊化されたバージョンが定義されます。
308
+ このような方法は標準ライブラリで細かく分類されたエラーを定義するために頻繁に使われています。
309
+ 一例を上げると < a href ="../std/io/type.Result.html "> io::Result</ a > がそれに当たります。</ p >
281
310
282
311
< script type ="text/javascript ">
283
312
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments