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 > Unsized Types </ title >
7
+ < title > サイズ不定型 </ 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 "> Unsized Types</ h1 >
189
- < p > Most types have a particular size, in bytes, that is knowable at compile time.
190
- For example, an < code > i32</ code > is thirty-two bits big, or four bytes. However, there are
191
- some types which are useful to express, but do not have a defined size. These are
192
- called ‘unsized’ or ‘dynamically sized’ types. One example is < code > [T]</ code > . This type
193
- represents a certain number of < code > T</ code > in sequence. But we don’t know how many
194
- there are, so the size is not known.</ p >
188
+ < h1 class ="title "> サイズ不定型</ h1 >
189
+ <!-- % Unsized Types -->
195
190
196
- < p > Rust understands a few of these types, but they have some restrictions. There
197
- are three:</ p >
191
+ <!-- Most types have a particular size, in bytes, that is knowable at compile time. -->
192
+
193
+ <!-- For example, an `i32` is thirty-two bits big, or four bytes. However, there are -->
194
+
195
+ <!-- some types which are useful to express, but do not have a defined size. These are -->
196
+
197
+ <!-- called ‘unsized’ or ‘dynamically sized’ types. One example is `[T]`. This type -->
198
+
199
+ <!-- represents a certain number of `T` in sequence. But we don’t know how many -->
200
+
201
+ <!-- there are, so the size is not known. -->
202
+
203
+ < p > ほとんどの型はコンパイル時に知れる、バイト数で測った、サイズがあります。
204
+ 例えば、 < code > i32</ code > 型は、32ビット(4バイト)というサイズです。
205
+ しかしながら、表現のためには便利であってもサイズが定まっていない型が存在します。
206
+ そのような方を 「サイズ不定」又は「動的サイズ」型と呼びます。
207
+ 一例を上げると < code > [T]</ code > 型は 一定のサイズの< code > T</ code > のシーケンスを意味していますが、その要素数については規定されていないため、サイズは不定となります。</ p >
208
+
209
+ <!-- Rust understands a few of these types, but they have some restrictions. There -->
210
+
211
+ <!-- are three: -->
212
+
213
+ < p > Rustはいくつかのそのような型を扱うことができますが、それらには以下の様な3つの制約が存在します:</ p >
214
+
215
+ <!-- 1. We can only manipulate an instance of an unsized type via a pointer. An ->
216
+ <!-- `&[T]` works just fine, but a `[T]` does not. -->
217
+
218
+ <!-- 2. Variables and arguments cannot have dynamically sized types. -->
219
+
220
+ <!-- 3. Only the last field in a `struct` may have a dynamically sized type; the -->
221
+
222
+ <!-- other fields must not. Enum variants must not have dynamically sized types as -->
223
+
224
+ <!-- data. -->
198
225
199
226
< ol >
200
- < li > We can only manipulate an instance of an unsized type via a pointer. An
201
- < code > &[T]</ code > works just fine, but a < code > [T]</ code > does not.</ li >
202
- < li > Variables and arguments cannot have dynamically sized types.</ li >
203
- < li > Only the last field in a < code > struct</ code > may have a dynamically sized type; the
204
- other fields must not. Enum variants must not have dynamically sized types as
205
- data.</ li >
227
+ < li > サイズ不定型はポインタを通してのみ操作することができます、たとえば、 < code > &[T]</ code > は大丈夫ですが、 < code > [T]</ code > はそうではありません。</ li >
228
+ < li > 変数や引数は動的なサイズを持つことはできません。</ li >
229
+ < li > < code > struct</ code > の最後のフィールドのみ、動的なサイズを持つことが許されます、その他のフィールドはサイズが不定であってはなりません。
230
+ また、Enumのバリアントはデータとして動的なサイズの型を持つ事はできません。</ li >
206
231
</ ol >
207
232
208
- < p > So why bother? Well, because < code > [T]</ code > can only be used behind a pointer, if we
209
- didn’t have language support for unsized types, it would be impossible to write
210
- this:</ p >
233
+ <!-- So why bother? Well, because `[T]` can only be used behind a pointer, if we -->
234
+
235
+ <!-- didn’t have language support for unsized types, it would be impossible to write -->
236
+
237
+ <!-- this: -->
238
+
239
+ < p > なぜこんなにややこしいのでしょうか? これは、< code > [T]</ code > はポインタを通してのみ操作可能であるため、
240
+ もし言語がサイズ不定型をサポートしていなかった場合、以下のようなコードを書くことは不可能となります:</ p >
211
241
< span class ='rusttest '> fn main() {
212
242
impl Foo for str {
213
243
}</ span > < pre class ='rust rust-example-rendered '>
214
244
< span class ='kw '> impl</ span > < span class ='ident '> Foo</ span > < span class ='kw '> for</ span > < span class ='ident '> str</ span > {</ pre >
215
245
216
- < p > or</ p >
246
+ <!-- or -->
247
+
248
+ < p > また、以下の様なコードも:</ p >
217
249
< span class ='rusttest '> fn main() {
218
250
impl<T> Foo for [T] {
219
251
}</ span > < pre class ='rust rust-example-rendered '>
220
252
< span class ='kw '> impl</ span > < span class ='op '> <</ span > < span class ='ident '> T</ span > < span class ='op '> ></ span > < span class ='ident '> Foo</ span > < span class ='kw '> for</ span > [< span class ='ident '> T</ span > ] {</ pre >
221
253
222
- < p > Instead, you would have to write:</ p >
254
+ <!-- Instead, you would have to write: -->
255
+
256
+ < p > このように書く代わりに、以下のように書く必要があることになるでしょう:</ p >
223
257
< span class ='rusttest '> fn main() {
224
258
impl Foo for &str {
225
259
}</ span > < pre class ='rust rust-example-rendered '>
226
260
< span class ='kw '> impl</ span > < span class ='ident '> Foo</ span > < span class ='kw '> for</ span > < span class ='kw-2 '> &</ span > < span class ='ident '> str</ span > {</ pre >
227
261
228
- < p > Meaning, this implementation would only work for < a href ="references-and-borrowing.html "> references</ a > , and not
229
- other types of pointers. With the < code > impl for str</ code > , all pointers, including (at
230
- some point, there are some bugs to fix first) user-defined custom smart
231
- pointers, can use this < code > impl</ code > .</ p >
262
+ <!-- Meaning, this implementation would only work for [references][ref], and not -->
263
+
264
+ <!-- other types of pointers. With the `impl for str`, all pointers, including (at -->
265
+
266
+ <!-- some point, there are some bugs to fix first) user-defined custom smart -->
267
+
268
+ <!-- pointers, can use this `impl`. -->
269
+
270
+ < p > このように書いたとすると、このコードは < a href ="references-and-borrowing.html "> 参照</ a > に対してのみ動作する用になり、他のポインタ型に対しては動作しないことになります。
271
+ < code > imp for str</ code > のように書くことで、すべてのポインタ、ユーザーの定義した独自のスマートポインタ(いくつかの点についてバグがあるので、それを先ずは直さなくてはなりませんが)もこの < code > impl</ code > を利用可能になります。</ p >
232
272
233
273
< h1 id ='sized ' class ='section-header '> < a href ='#sized '> ?Sized</ a > </ h1 >
234
- < p > If you want to write a function that accepts a dynamically sized type, you
235
- can use the special bound, < code > ?Sized</ code > :</ p >
274
+ <!-- If you want to write a function that accepts a dynamically sized type, you -->
275
+
276
+ <!-- can use the special bound, `?Sized`: -->
277
+
278
+ < p > もし動的サイズ型を引数に取れるような関数を定義したい場合、特別な境界 < code > ?Sized</ code > を利用できます:</ p >
236
279
< span class ='rusttest '> fn main() {
237
280
struct Foo<T: ?Sized> {
238
281
f: T,
@@ -242,9 +285,14 @@ <h1 id='sized' class='section-header'><a href='#sized'>?Sized</a></h1>
242
285
< span class ='ident '> f</ span > : < span class ='ident '> T</ span > ,
243
286
}</ pre >
244
287
245
- < p > This < code > ?</ code > , read as “T may be < code > Sized</ code > ”, means that this bound is special: it
246
- lets us match more kinds, not less. It’s almost like every < code > T</ code > implicitly has
247
- < code > T: Sized</ code > , and the < code > ?</ code > undoes this default.</ p >
288
+ <!-- This `?`, read as “T may be `Sized`”, means that this bound is special: it -->
289
+
290
+ <!-- lets us match more kinds, not less. It’s almost like every `T` implicitly has -->
291
+
292
+ <!-- `T: Sized`, and the `?` undoes this default. -->
293
+
294
+ < p > < code > ?</ code > は 「Tは < code > Sized</ code > かもしれない」と読みます、これは < code > ?</ code > が特別な境界: より小さいカインドとマッチするのではなく、より大きいカインドとマッチする ということを意味しています。
295
+ これは、すべての < code > T</ code > は暗黙的に < code > T : Sized</ code > という制限がかけられていて、 < code > ?</ code > はその制限を解除するというようなものです。</ p >
248
296
249
297
< script type ="text/javascript ">
250
298
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments