1
- % Mutability
1
+ % ミュータビリティ
2
+ <!-- % Mutability -->
2
3
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
+ ミュータビリティの一つ目の特徴は、それがデフォルトでは無いという点です:
6
9
7
10
``` rust,ignore
8
11
let x = 5;
9
- x = 6; // error!
12
+ # // x = 6; // error!
13
+ x = 6; // エラー!
10
14
```
11
15
12
- We can introduce mutability with the ` mut ` keyword:
16
+ <!-- We can introduce mutability with the `mut` keyword: -->
17
+ ` mut ` キーワードによりミュータビリティを導入できます:
13
18
14
19
``` rust
15
20
let mut x = 5 ;
16
21
17
- x = 6 ; // no problem!
22
+ # // x = 6; // no problem!
23
+ x = 6 ; // 問題なし!
18
24
```
19
25
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 ` から別の値へと束縛が変わったのです。
24
32
25
33
[ vb ] : variable-bindings.html
26
34
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 ] を使う必要があるでしょう:
28
37
29
38
``` rust
30
39
let mut x = 5 ;
@@ -33,22 +42,28 @@ let y = &mut x;
33
42
34
43
[ mr ] : references-and-borrowing.html
35
44
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 ` )は可能です。微妙な区別です。
39
50
40
- Of course, if you need both:
51
+ <!-- Of course, if you need both: -->
52
+ もちろん、両方が必要ならば:
41
53
42
54
``` rust
43
55
let mut x = 5 ;
44
56
let mut y = & mut x ;
45
57
```
46
58
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 ` が他の値を束縛することもできますし、参照している値を変更することもできます。
49
62
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
+ つまり、次のようなことが可能です:
52
67
53
68
``` rust
54
69
let (mut x , y ) = (5 , 6 );
@@ -59,11 +74,14 @@ fn foo(mut x: i32) {
59
74
60
75
[ pattern ] : patterns.html
61
76
62
- # Interior vs. Exterior Mutability
77
+ <!-- # Interior vs. Exterior Mutability -->
78
+ # 内側 vs. 外側のミュータビリティ
63
79
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 ] を考えます:
67
85
68
86
``` rust
69
87
use std :: sync :: Arc ;
@@ -74,30 +92,46 @@ let y = x.clone();
74
92
75
93
[ arc ] : ../std/sync/struct.Arc.html
76
94
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つを持つことはありますが、両方を同時に持つことはありません。
87
114
>
88
- > * one or more references ( ` &T ` ) to a resource,
89
- > * exactly one mutable reference ( ` &mut T ` ).
115
+ > * リソースに対する1つ以上の参照( ` &T ` )
116
+ > * ただ1つのミュータブルな参照( ` &mut T ` )
90
117
91
118
[ ownership ] : ownership.html
92
119
[ borrowing ] : references-and-borrowing.html#borrowing
93
120
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
+ 例えば:
101
135
102
136
``` rust
103
137
use std :: cell :: RefCell ;
@@ -109,8 +143,10 @@ let y = x.borrow_mut();
109
143
110
144
[ stdcell ] : ../std/cell/index.html
111
145
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
+ それって危ないのでは? もし次のようにすると:
114
150
115
151
``` rust,ignore
116
152
use std::cell::RefCell;
@@ -122,25 +158,35 @@ let z = x.borrow_mut();
122
158
# (y, z);
123
159
```
124
160
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
+ ## フィールド・レベルのミュータビリティ
129
172
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 ] は作れないということです。
131
178
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:
135
179
136
180
``` rust,ignore
137
181
struct Point {
138
182
x: i32,
139
- mut y: i32, // nope
183
+ # // mut y: i32, // nope
184
+ mut y: i32, // ダメ
140
185
}
141
186
```
142
187
143
- The mutability of a struct is in its binding:
188
+ <!-- The mutability of a struct is in its binding: -->
189
+ 構造体のミュータビリティは、それへの束縛の一部です。
144
190
145
191
``` rust,ignore
146
192
struct Point {
@@ -154,12 +200,14 @@ a.x = 10;
154
200
155
201
let b = Point { x: 5, y: 6};
156
202
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` へ代入できない
158
205
```
159
206
160
207
[ struct ] : structs.html
161
208
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 ] を使えば、フィールド・レベルのミュータビリティをエミュレートできます。
163
211
164
212
``` rust
165
213
use std :: cell :: Cell ;
@@ -178,4 +226,5 @@ println!("y: {:?}", point.y);
178
226
179
227
[ cell ] : ../std/cell/struct.Cell.html
180
228
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 ` を更新できました。
0 commit comments