5
5
<!-- than in other languages. The first aspect of mutability is its non-default -->
6
6
<!-- status: -->
7
7
Rustにおけるミュータビリティ、何かを変更する能力は、他のプログラミング言語とはすこし異なっています。
8
- ミュータビリティの一つ目の特徴は、デフォルトでは無いという点です :
8
+ ミュータビリティの一つ目の特徴は、それがデフォルトでは無いという点です :
9
9
10
10
``` rust,ignore
11
11
let x = 5;
12
- x = 6; // error!
12
+ # // x = 6; // error!
13
+ x = 6; // エラー!
13
14
```
14
15
15
16
<!-- We can introduce mutability with the `mut` keyword: -->
@@ -18,20 +19,21 @@ x = 6; // error!
18
19
``` rust
19
20
let mut x = 5 ;
20
21
21
- x = 6 ; // no problem!
22
+ # // x = 6; // no problem!
23
+ x = 6 ; // 問題なし!
22
24
```
23
25
24
26
<!-- This is a mutable [variable binding][vb]. When a binding is mutable, it means -->
25
27
<!-- you’re allowed to change what the binding points to. So in the above example, -->
26
28
<!-- it’s not so much that the value at `x` is changing, but that the binding -->
27
29
<!-- changed from one `i32` to another. -->
28
- これはミュータブルな [ 変数束縛] [ vb ] です。束縛がミュータブルのとき、束縛が何を指すかを変更して良いことを意味します 。
29
- つまり上記の例では、 ` x ` の値を変更したのではなく、束縛がある ` i32 ` から別のものへ変わったのです 。
30
+ これはミュータブルな [ 変数束縛] [ vb ] です。束縛がミュータブルであるとき、その束縛が何を指すかを変更して良いことを意味します 。
31
+ つまり上記の例では、` x ` の値を変更したのではなく、ある ` i32 ` から別の値へと束縛が変わったのです 。
30
32
31
33
[ vb ] : variable-bindings.html
32
34
33
35
<!-- If you want to change what the binding points to, you’ll need a [mutable reference][mr]: -->
34
- 束縛先を変更したいのであれば、 [ ミュータブル参照] [ mr ] を使うことができます :
36
+ 束縛が指す先を変更する場合は、 [ ミュータブル参照] [ mr ] を使う必要があるでしょう :
35
37
36
38
``` rust
37
39
let mut x = 5 ;
@@ -43,7 +45,7 @@ let y = &mut x;
43
45
<!-- `y` is an immutable binding to a mutable reference, which means that you can’t -->
44
46
<!-- bind `y` to something else (`y = &mut z`), but you can mutate the thing that’s -->
45
47
<!-- bound to `y` (`*y = 5`). A subtle distinction. -->
46
- ` y ` はミュータブル参照へのイミュータブル束縛であり 、 ` y ` の束縛先を他のものに変える (` y = &mut z ` )ことはできません。
48
+ ` y ` はミュータブル参照へのイミュータブルな束縛であり 、 ` y ` を他の束縛に変える (` y = &mut z ` )ことはできません。
47
49
しかし、` y ` に束縛されているものを変化させること(` *y = 5 ` )は可能です。
48
50
49
51
<!-- Of course, if you need both: -->
@@ -60,8 +62,8 @@ let mut y = &mut x;
60
62
61
63
<!-- It’s important to note that `mut` is part of a [pattern][pattern], so you -->
62
64
<!-- can do things like this: -->
63
- ` mut ` は [ パターン] [ pattern ] の一部であることに十分注意してください 。
64
- つまり、次のようなことを行えます :
65
+ ` mut ` は [ パターン] [ pattern ] の一部を成すことに十分注意してください 。
66
+ つまり、次のようなことが可能です :
65
67
66
68
``` rust
67
69
let (mut x , y ) = (5 , 6 );
@@ -79,7 +81,7 @@ fn foo(mut x: i32) {
79
81
<!-- it’s not able to be changed: we mean something has ‘exterior mutability’. Consider, -->
80
82
<!-- for example, [`Arc<T>`][arc]: -->
81
83
一方で、Rustで「イミュータブル(immutable)」について言及するとき、変更不可能であることを意味しない:
82
- 「外側のミュータビリティ(exterior mutability)」を表します。例として、 [ ` Arc<T> ` ] [ arc ] を考えます:
84
+ 「外側のミュータビリティ(exterior mutability)」を表します。例として、[ ` Arc<T> ` ] [ arc ] を考えます:
83
85
84
86
``` rust
85
87
use std :: sync :: Arc ;
@@ -94,13 +96,13 @@ let y = x.clone();
94
96
<!-- we’ve not used any `mut`s here, `x` is an immutable binding, and we didn’t take -->
95
97
<!-- `&mut 5` or anything. So what gives? -->
96
98
` clone() ` を呼び出すとき、` Arc<T> ` は参照カウントを更新する必要があります。しかし、
97
- ここでは ` mut ` を一切使っていません。つまり ` x ` はイミュータブル束縛であり 、
98
- ` &mut 5 ` のような引数もとりません。一体どうなっているの?
99
+ ここでは ` mut ` を一切使っていません。つまり ` x ` はイミュータブルな束縛であり 、
100
+ ` &mut 5 ` のような引数もとりません。一体どうなっているの?
99
101
100
102
<!-- To understand this, we have to go back to the core of Rust’s guiding -->
101
103
<!-- philosophy, memory safety, and the mechanism by which Rust guarantees it, the -->
102
104
<!-- [ownership][ownership] system, and more specifically, [borrowing][borrowing]: -->
103
- これを理解するには、Rust言語の設計哲学の中心をなすメモリ安全性 、Rustがそれを保証するメカニズムである [ 所有権] [ ownership ] システム、
105
+ これを理解するには、Rust言語の設計哲学の中心をなすメモリ安全性と 、Rustがそれを保証するメカニズムである [ 所有権] [ ownership ] システム、
104
106
特に [ 借用] [ borrowing ] に立ち返る必要があります。
105
107
106
108
<!-- > You may have one or the other of these two kinds of borrows, but not both at -->
@@ -120,14 +122,15 @@ let y = x.clone();
120
122
<!-- pointers to? In `Arc<T>`’s case, yes: the mutation is entirely contained inside -->
121
123
<!-- the structure itself. It’s not user facing. For this reason, it hands out `&T` -->
122
124
<!-- with `clone()`. If it handed out `&mut T`s, though, that would be a problem. -->
123
- つまり、「イミュータビリティ」の真の定義はこうです: これは2つから指されても安全か ?
124
- ` Arc<T> ` の例では、イエス: 変更は完全それ自身の構造の内側で行われます 。ユーザ向いではありません。
125
+ つまり、「イミュータビリティ」の真の定義はこうです: これは2箇所から指されても安全ですか ?
126
+ ` Arc<T> ` の例では、イエス: 変更は完全にそれ自身の構造の内側で行われます 。ユーザ向いではありません。
125
127
このような理由により、 ` clone() ` を用いて ` T& ` を配るのです。仮に ` &mut T ` を配ってしまうと、
126
- 問題になるでしょう。(訳注: ` Arc<T> ` を用いて複数スレッドへイミュータブルな値を配布する。)
128
+ 問題になるでしょう。
129
+ (訳注: ` Arc<T> ` を用いて複数スレッドにイミュータブル参照を配布し、スレッド間でオブジェクトを共有できます。)
127
130
128
131
<!-- Other types, like the ones in the [`std::cell`][stdcell] module, have the -->
129
132
<!-- opposite: interior mutability. For example: -->
130
- [ ` std::cell ` ] [ stdcell ] モジュール中の他の型は 、反対の性質: 内側のミュータビリティを持ちます 。
133
+ [ ` std::cell ` ] [ stdcell ] モジュールにあるような別の型では 、反対の性質: 内側のミュータビリティ(interior mutability)を持ちます 。
131
134
例えば:
132
135
133
136
``` rust
@@ -140,8 +143,10 @@ let y = x.borrow_mut();
140
143
141
144
[ stdcell ] : ../std/cell/index.html
142
145
143
- RefCell hands out ` &mut ` references to what’s inside of it with the
144
- ` 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
+ それって危ないのでは? もし次のようにすると:
145
150
146
151
``` rust,ignore
147
152
use std::cell::RefCell;
@@ -153,25 +158,35 @@ let z = x.borrow_mut();
153
158
# (y, z);
154
159
```
155
160
156
- This will in fact panic, at runtime. This is what ` RefCell ` does: it enforces
157
- Rust’s borrowing rules at runtime, and ` panic! ` s if they’re violated. This
158
- allows us to get around another aspect of Rust’s mutability rules. Let’s talk
159
- 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
+ 最初に見ていきましょう。
160
169
161
- ## Field-level mutability
170
+ <!-- ## Field-level mutability -->
171
+ ## フィールド・レベルのミュータビリティ
172
+
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 ] は作れないということです。
162
178
163
- Mutability is a property of either a borrow (` &mut ` ) or a binding (` let mut ` ).
164
- This means that, for example, you cannot have a [ ` struct ` ] [ struct ] with
165
- some fields mutable and some immutable:
166
179
167
180
``` rust,ignore
168
181
struct Point {
169
182
x: i32,
170
- mut y: i32, // nope
183
+ # // mut y: i32, // nope
184
+ mut y: i32, // ダメ
171
185
}
172
186
```
173
187
174
- The mutability of a struct is in its binding:
188
+ <!-- The mutability of a struct is in its binding: -->
189
+ 構造体のミュータビリティは、それへの束縛の一部です。
175
190
176
191
``` rust,ignore
177
192
struct Point {
@@ -185,12 +200,14 @@ a.x = 10;
185
200
186
201
let b = Point { x: 5, y: 6};
187
202
188
- 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` へ代入できない
189
205
```
190
206
191
207
[ struct ] : structs.html
192
208
193
- 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 ] を使えば、フィールド・レベルのミュータビリティをエミュレートできます。
194
211
195
212
``` rust
196
213
use std :: cell :: Cell ;
@@ -209,4 +226,5 @@ println!("y: {:?}", point.y);
209
226
210
227
[ cell ] : ../std/cell/struct.Cell.html
211
228
212
- 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