Skip to content

Commit e741b9e

Browse files
committed
Merge pull request #83 from yohhoy/mutability
4.10 Mutability
2 parents ff8b2b0 + 85daffe commit e741b9e

File tree

2 files changed

+111
-58
lines changed

2 files changed

+111
-58
lines changed

1.6/ja/book/mutability.md

Lines changed: 107 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1-
% Mutability
1+
% ミュータビリティ
2+
<!-- % Mutability -->
23

3-
Mutability, the ability to change something, works a bit differently in Rust
4-
than in other languages. The first aspect of mutability is its non-default
5-
status:
4+
<!-- Mutability, the ability to change something, works a bit differently in Rust -->
5+
<!-- than in other languages. The first aspect of mutability is its non-default -->
6+
<!-- status: -->
7+
Rustにおけるミュータビリティ、何かを変更する能力は、他のプログラミング言語とはすこし異なっています。
8+
ミュータビリティの一つ目の特徴は、それがデフォルトでは無いという点です:
69

710
```rust,ignore
811
let x = 5;
9-
x = 6; // error!
12+
# // x = 6; // error!
13+
x = 6; // エラー!
1014
```
1115

12-
We can introduce mutability with the `mut` keyword:
16+
<!-- We can introduce mutability with the `mut` keyword: -->
17+
`mut` キーワードによりミュータビリティを導入できます:
1318

1419
```rust
1520
let mut x = 5;
1621

17-
x = 6; // no problem!
22+
# // x = 6; // no problem!
23+
x = 6; // 問題なし!
1824
```
1925

20-
This is a mutable [variable binding][vb]. When a binding is mutable, it means
21-
you’re allowed to change what the binding points to. So in the above example,
22-
it’s not so much that the value at `x` is changing, but that the binding
23-
changed from one `i32` to another.
26+
<!-- This is a mutable [variable binding][vb]. When a binding is mutable, it means -->
27+
<!-- you’re allowed to change what the binding points to. So in the above example, -->
28+
<!-- it’s not so much that the value at `x` is changing, but that the binding -->
29+
<!-- changed from one `i32` to another. -->
30+
これはミュータブルな [変数束縛][vb] です。束縛がミュータブルであるとき、その束縛が何を指すかを変更して良いことを意味します。
31+
つまり上記の例では、`x` の値を変更したのではなく、ある `i32` から別の値へと束縛が変わったのです。
2432

2533
[vb]: variable-bindings.html
2634

27-
If you want to change what the binding points to, you’ll need a [mutable reference][mr]:
35+
<!-- If you want to change what the binding points to, you’ll need a [mutable reference][mr]: -->
36+
束縛が指す先を変更する場合は、[ミュータブル参照][mr] を使う必要があるでしょう:
2837

2938
```rust
3039
let mut x = 5;
@@ -33,22 +42,28 @@ let y = &mut x;
3342

3443
[mr]: references-and-borrowing.html
3544

36-
`y` is an immutable binding to a mutable reference, which means that you can’t
37-
bind `y` to something else (`y = &mut z`), but you can mutate the thing that’s
38-
bound to `y` (`*y = 5`). A subtle distinction.
45+
<!-- `y` is an immutable binding to a mutable reference, which means that you can’t -->
46+
<!-- bind `y` to something else (`y = &mut z`), but you can mutate the thing that’s -->
47+
<!-- bound to `y` (`*y = 5`). A subtle distinction. -->
48+
`y` はミュータブル参照へのイミュータブルな束縛であり、 `y` を他の束縛に変える(`y = &mut z`)ことはできません。
49+
しかし、`y` に束縛されているものを変化させること(`*y = 5`)は可能です。微妙な区別です。
3950

40-
Of course, if you need both:
51+
<!-- Of course, if you need both: -->
52+
もちろん、両方が必要ならば:
4153

4254
```rust
4355
let mut x = 5;
4456
let mut y = &mut x;
4557
```
4658

47-
Now `y` can be bound to another value, and the value it’s referencing can be
48-
changed.
59+
<!-- Now `y` can be bound to another value, and the value it’s referencing can be -->
60+
<!-- changed. -->
61+
今度は `y` が他の値を束縛することもできますし、参照している値を変更することもできます。
4962

50-
It’s important to note that `mut` is part of a [pattern][pattern], so you
51-
can do things like this:
63+
<!-- It’s important to note that `mut` is part of a [pattern][pattern], so you -->
64+
<!-- can do things like this: -->
65+
`mut`[パターン][pattern] の一部を成すことに十分注意してください。
66+
つまり、次のようなことが可能です:
5267

5368
```rust
5469
let (mut x, y) = (5, 6);
@@ -59,11 +74,14 @@ fn foo(mut x: i32) {
5974

6075
[pattern]: patterns.html
6176

62-
# Interior vs. Exterior Mutability
77+
<!-- # Interior vs. Exterior Mutability -->
78+
# 内側 vs. 外側のミュータビリティ
6379

64-
However, when we say something is ‘immutable’ in Rust, that doesn’t mean that
65-
it’s not able to be changed: we mean something has ‘exterior mutability’. Consider,
66-
for example, [`Arc<T>`][arc]:
80+
<!-- However, when we say something is ‘immutable’ in Rust, that doesn’t mean that -->
81+
<!-- it’s not able to be changed: we mean something has ‘exterior mutability’. Consider, -->
82+
<!-- for example, [`Arc<T>`][arc]: -->
83+
一方で、Rustで「イミュータブル(immutable)」について言及するとき、変更不可能であることを意味しない:
84+
「外側のミュータビリティ(exterior mutability)」を表します。例として、[`Arc<T>`][arc] を考えます:
6785

6886
```rust
6987
use std::sync::Arc;
@@ -74,30 +92,46 @@ let y = x.clone();
7492

7593
[arc]: ../std/sync/struct.Arc.html
7694

77-
When we call `clone()`, the `Arc<T>` needs to update the reference count. Yet
78-
we’ve not used any `mut`s here, `x` is an immutable binding, and we didn’t take
79-
`&mut 5` or anything. So what gives?
80-
81-
To understand this, we have to go back to the core of Rust’s guiding
82-
philosophy, memory safety, and the mechanism by which Rust guarantees it, the
83-
[ownership][ownership] system, and more specifically, [borrowing][borrowing]:
84-
85-
> You may have one or the other of these two kinds of borrows, but not both at
86-
> the same time:
95+
<!-- When we call `clone()`, the `Arc<T>` needs to update the reference count. Yet -->
96+
<!-- we’ve not used any `mut`s here, `x` is an immutable binding, and we didn’t take -->
97+
<!-- `&mut 5` or anything. So what gives? -->
98+
`clone()` を呼び出すとき、`Arc<T>` は参照カウントを更新する必要があります。しかし、
99+
ここでは `mut` を一切使っていません。つまり `x` はイミュータブルな束縛であり、
100+
`&mut 5` のような引数もとりません。一体どうなっているの?
101+
102+
<!-- To understand this, we have to go back to the core of Rust’s guiding -->
103+
<!-- philosophy, memory safety, and the mechanism by which Rust guarantees it, the -->
104+
<!-- [ownership][ownership] system, and more specifically, [borrowing][borrowing]: -->
105+
これを理解するには、Rust言語の設計哲学の中心をなすメモリ安全性と、Rustがそれを保証するメカニズムである [所有権][ownership] システム、
106+
特に [借用][borrowing] に立ち返る必要があります。
107+
108+
<!-- > You may have one or the other of these two kinds of borrows, but not both at -->
109+
<!-- > the same time: -->
110+
<!-- > -->
111+
<!-- > * one or more references (`&T`) to a resource, -->
112+
<!-- > * exactly one mutable reference (`&mut T`). -->
113+
> 次の2種類の借用のどちらか1つを持つことはありますが、両方を同時に持つことはありません。
87114
>
88-
> * one or more references (`&T`) to a resource,
89-
> * exactly one mutable reference (`&mut T`).
115+
> * リソースに対する1つ以上の参照(`&T`
116+
> * ただ1つのミュータブルな参照(`&mut T`
90117
91118
[ownership]: ownership.html
92119
[borrowing]: references-and-borrowing.html#borrowing
93120

94-
So, that’s the real definition of ‘immutability’: is this safe to have two
95-
pointers to? In `Arc<T>`’s case, yes: the mutation is entirely contained inside
96-
the structure itself. It’s not user facing. For this reason, it hands out `&T`
97-
with `clone()`. If it handed out `&mut T`s, though, that would be a problem.
98-
99-
Other types, like the ones in the [`std::cell`][stdcell] module, have the
100-
opposite: interior mutability. For example:
121+
<!-- So, that’s the real definition of ‘immutability’: is this safe to have two -->
122+
<!-- pointers to? In `Arc<T>`’s case, yes: the mutation is entirely contained inside -->
123+
<!-- the structure itself. It’s not user facing. For this reason, it hands out `&T` -->
124+
<!-- with `clone()`. If it handed out `&mut T`s, though, that would be a problem. -->
125+
つまり、「イミュータビリティ」の真の定義はこうです: これは2箇所から指されても安全ですか?
126+
`Arc<T>` の例では、イエス: 変更は完全にそれ自身の構造の内側で行われます。ユーザからは見えません。
127+
このような理由により、 `clone()` を用いて `T&` を配るのです。仮に `&mut T` を配ってしまうと、
128+
問題になるでしょう。
129+
(訳注: `Arc<T>`を用いて複数スレッドにイミュータブル参照を配布し、スレッド間でオブジェクトを共有できます。)
130+
131+
<!-- Other types, like the ones in the [`std::cell`][stdcell] module, have the -->
132+
<!-- opposite: interior mutability. For example: -->
133+
[`std::cell`][stdcell] モジュールにあるような別の型では、反対の性質: 内側のミュータビリティ(interior mutability)を持ちます。
134+
例えば:
101135

102136
```rust
103137
use std::cell::RefCell;
@@ -109,8 +143,10 @@ let y = x.borrow_mut();
109143

110144
[stdcell]: ../std/cell/index.html
111145

112-
RefCell hands out `&mut` references to what’s inside of it with the
113-
`borrow_mut()` method. Isn’t that dangerous? What if we do:
146+
<!-- RefCell hands out `&mut` references to what’s inside of it with the -->
147+
<!-- `borrow_mut()` method. Isn’t that dangerous? What if we do: -->
148+
RefCellでは `borrow_mut()` メソッドによって、その内側にある値への `&mut` 参照を配ります。
149+
それって危ないのでは? もし次のようにすると:
114150

115151
```rust,ignore
116152
use std::cell::RefCell;
@@ -122,25 +158,35 @@ let z = x.borrow_mut();
122158
# (y, z);
123159
```
124160

125-
This will in fact panic, at runtime. This is what `RefCell` does: it enforces
126-
Rust’s borrowing rules at runtime, and `panic!`s if they’re violated. This
127-
allows us to get around another aspect of Rust’s mutability rules. Let’s talk
128-
about it first.
161+
<!-- This will in fact panic, at runtime. This is what `RefCell` does: it enforces -->
162+
<!-- Rust’s borrowing rules at runtime, and `panic!`s if they’re violated. This -->
163+
<!-- allows us to get around another aspect of Rust’s mutability rules. Let’s talk -->
164+
<!-- about it first. -->
165+
実際に、このコードは実行時にパニックするでしょう。これが `RefCell` が行うことです:
166+
Rustの借用ルールを実行時に強制し、違反したときには `panic!` を呼び出します。
167+
これによりRustのミュータビリティ・ルールのもう一つの特徴を回避できるようになります。
168+
最初に見ていきましょう。
169+
170+
<!-- ## Field-level mutability -->
171+
## フィールド・レベルのミュータビリティ
129172

130-
## Field-level mutability
173+
<!-- Mutability is a property of either a borrow (`&mut`) or a binding (`let mut`). -->
174+
<!-- This means that, for example, you cannot have a [`struct`][struct] with -->
175+
<!-- some fields mutable and some immutable: -->
176+
ミュータビリティとは、借用(`&mut`)や束縛(`let mut`)に関する属性です。これが意味するのは、
177+
例えば、一部がミュータブルで一部がイミュータブルなフィールドを持つ [`struct`][struct] は作れないということです。
131178

132-
Mutability is a property of either a borrow (`&mut`) or a binding (`let mut`).
133-
This means that, for example, you cannot have a [`struct`][struct] with
134-
some fields mutable and some immutable:
135179

136180
```rust,ignore
137181
struct Point {
138182
x: i32,
139-
mut y: i32, // nope
183+
# // mut y: i32, // nope
184+
mut y: i32, // ダメ
140185
}
141186
```
142187

143-
The mutability of a struct is in its binding:
188+
<!-- The mutability of a struct is in its binding: -->
189+
構造体のミュータビリティは、それへの束縛の一部です。
144190

145191
```rust,ignore
146192
struct Point {
@@ -154,12 +200,14 @@ a.x = 10;
154200
155201
let b = Point { x: 5, y: 6};
156202
157-
b.x = 10; // error: cannot assign to immutable field `b.x`
203+
# // b.x = 10; // error: cannot assign to immutable field `b.x`
204+
b.x = 10; // エラー: イミュータブルなフィールド `b.x` へ代入できない
158205
```
159206

160207
[struct]: structs.html
161208

162-
However, by using [`Cell<T>`][cell], you can emulate field-level mutability:
209+
<!-- However, by using [`Cell<T>`][cell], you can emulate field-level mutability: -->
210+
しかし、[`Cell<T>`][cell] を使えば、フィールド・レベルのミュータビリティをエミュレートできます。
163211

164212
```rust
165213
use std::cell::Cell;
@@ -178,4 +226,5 @@ println!("y: {:?}", point.y);
178226

179227
[cell]: ../std/cell/struct.Cell.html
180228

181-
This will print `y: Cell { value: 7 }`. We’ve successfully updated `y`.
229+
<!-- This will print `y: Cell { value: 7 }`. We’ve successfully updated `y`. -->
230+
このコードは `y: Cell { value: 7 }` と表示するでしょう。ちゃんと `y` を更新できました。

TranslationTable.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
| error handling | エラーハンドリング
7171
| existentially quantified type | 存在量型
7272
| expression statement | 式文
73+
| exterior | 外側の
7374
| feature | フィーチャ
7475
| foreign | 他言語
7576
| free-standing function | フリースタンディングな関数
@@ -81,9 +82,11 @@
8182
| hash | ハッシュ
8283
| identifier | 識別子
8384
| immutable | イミュータブル
85+
| immutability | イミュータビリティ
8486
| implement | 実装する
8587
| initialize | 初期化する
8688
| input lifetime | 入力ライフタイム
89+
| interior | 内側の
8790
| install | インストール
8891
| installer | インストーラ
8992
| interpolate | インターポーレートする
@@ -137,6 +140,7 @@
137140
| return | 返す
138141
| return type | リターン型
139142
| return value | 戻り値
143+
| runtime | 実行時
140144
| safe | 安全
141145
| safety check | 安全性検査
142146
| scope | スコープ

0 commit comments

Comments
 (0)