Skip to content

Commit dcda24d

Browse files
committed
Merge pull request #66 from pocket7878/unsized-type
Unsized Types
2 parents bd6ff38 + 3457110 commit dcda24d

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

1.6/ja/book/unsized-types.md

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,80 @@
1-
% Unsized Types
1+
% サイズ不定型
2+
<!-- % Unsized Types -->
23

3-
Most types have a particular size, in bytes, that is knowable at compile time.
4-
For example, an `i32` is thirty-two bits big, or four bytes. However, there are
5-
some types which are useful to express, but do not have a defined size. These are
6-
called ‘unsized’ or ‘dynamically sized’ types. One example is `[T]`. This type
7-
represents a certain number of `T` in sequence. But we don’t know how many
8-
there are, so the size is not known.
4+
<!-- Most types have a particular size, in bytes, that is knowable at compile time. -->
5+
<!-- For example, an `i32` is thirty-two bits big, or four bytes. However, there are -->
6+
<!-- some types which are useful to express, but do not have a defined size. These are -->
7+
<!-- called ‘unsized’ or ‘dynamically sized’ types. One example is `[T]`. This type -->
8+
<!-- represents a certain number of `T` in sequence. But we don’t know how many -->
9+
<!-- there are, so the size is not known. -->
10+
ほとんどの型はコンパイル時に知れる、バイト数で測った、サイズがあります。
11+
例えば、 `i32` 型は、32ビット(4バイト)というサイズです。
12+
しかしながら、表現のためには便利であってもサイズが定まっていない型が存在します。
13+
そのような方を 「サイズ不定」又は「動的サイズ」型と呼びます。
14+
一例を上げると `[T]` 型は 一定のサイズの`T` のシーケンスを意味していますが、その要素数については規定されていないため、サイズは不定となります。
915

10-
Rust understands a few of these types, but they have some restrictions. There
11-
are three:
16+
<!-- Rust understands a few of these types, but they have some restrictions. There -->
17+
<!-- are three: -->
18+
Rustはいくつかのそのような型を扱うことができますが、それらには以下の様な3つの制約が存在します:
1219

13-
1. We can only manipulate an instance of an unsized type via a pointer. An
14-
`&[T]` works just fine, but a `[T]` does not.
15-
2. Variables and arguments cannot have dynamically sized types.
16-
3. Only the last field in a `struct` may have a dynamically sized type; the
17-
other fields must not. Enum variants must not have dynamically sized types as
18-
data.
20+
<!-- 1. We can only manipulate an instance of an unsized type via a pointer. An ->
21+
<!-- `&[T]` works just fine, but a `[T]` does not. -->
22+
<!-- 2. Variables and arguments cannot have dynamically sized types. -->
23+
<!-- 3. Only the last field in a `struct` may have a dynamically sized type; the -->
24+
<!-- other fields must not. Enum variants must not have dynamically sized types as -->
25+
<!-- data. -->
26+
1. サイズ不定型はポインタを通してのみ操作することができます、たとえば、 `&[T]` は大丈夫ですが、 `[T]` はそうではありません。
27+
2. 変数や引数は動的なサイズを持つことはできません。
28+
3. `struct` の最後のフィールドのみ、動的なサイズを持つことが許されます、その他のフィールドはサイズが不定であってはなりません。
29+
また、Enumのバリアントはデータとして動的なサイズの型を持つ事はできません。
1930

20-
So why bother? Well, because `[T]` can only be used behind a pointer, if we
21-
didn’t have language support for unsized types, it would be impossible to write
22-
this:
31+
<!-- So why bother? Well, because `[T]` can only be used behind a pointer, if we -->
32+
<!-- didn’t have language support for unsized types, it would be impossible to write -->
33+
<!-- this: -->
34+
なぜこんなにややこしいのでしょうか? これは、`[T]` はポインタを通してのみ操作可能であるため、
35+
もし言語がサイズ不定型をサポートしていなかった場合、以下のようなコードを書くことは不可能となります:
2336

2437
```rust,ignore
2538
impl Foo for str {
2639
```
2740

28-
or
41+
<!-- or -->
42+
また、以下の様なコードも:
2943

3044
```rust,ignore
3145
impl<T> Foo for [T] {
3246
```
3347

34-
Instead, you would have to write:
48+
<!-- Instead, you would have to write: -->
49+
このように書く代わりに、以下のように書く必要があることになるでしょう:
3550

3651
```rust,ignore
3752
impl Foo for &str {
3853
```
3954

40-
Meaning, this implementation would only work for [references][ref], and not
41-
other types of pointers. With the `impl for str`, all pointers, including (at
42-
some point, there are some bugs to fix first) user-defined custom smart
43-
pointers, can use this `impl`.
55+
<!-- Meaning, this implementation would only work for [references][ref], and not -->
56+
<!-- other types of pointers. With the `impl for str`, all pointers, including (at -->
57+
<!-- some point, there are some bugs to fix first) user-defined custom smart -->
58+
<!-- pointers, can use this `impl`. -->
59+
このように書いたとすると、このコードは [参照][ref] に対してのみ動作する用になり、他のポインタ型に対しては動作しないことになります。
60+
`imp for str` のように書くことで、すべてのポインタ、ユーザーの定義した独自のスマートポインタ(いくつかの点についてバグがあるので、それを先ずは直さなくてはなりませんが)もこの `impl` を利用可能になります。
4461

4562
[ref]: references-and-borrowing.html
4663

4764
# ?Sized
4865

49-
If you want to write a function that accepts a dynamically sized type, you
50-
can use the special bound, `?Sized`:
66+
<!-- If you want to write a function that accepts a dynamically sized type, you -->
67+
<!-- can use the special bound, `?Sized`: -->
68+
もし動的サイズ型を引数に取れるような関数を定義したい場合、特別な境界 `?Sized` を利用できます:
5169

5270
```rust
5371
struct Foo<T: ?Sized> {
5472
f: T,
5573
}
5674
```
5775

58-
This `?`, read as “T may be `Sized`”, means that this bound is special: it
59-
lets us match more kinds, not less. It’s almost like every `T` implicitly has
60-
`T: Sized`, and the `?` undoes this default.
76+
<!-- This `?`, read as “T may be `Sized`”, means that this bound is special: it -->
77+
<!-- lets us match more kinds, not less. It’s almost like every `T` implicitly has -->
78+
<!-- `T: Sized`, and the `?` undoes this default. -->
79+
`?` は 「Tは `Sized` かもしれない」と読みます、これは `?` が特別な境界: より小さいカインドとマッチするのではなく、より大きいカインドとマッチする ということを意味しています。
80+
これは、すべての `T` は暗黙的に `T : Sized` という制限がかけられていて、 `?` はその制限を解除するというようなものです。

0 commit comments

Comments
 (0)