1
1
% ミュータビリティ
2
2
<!-- % Mutability -->
3
3
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:
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
+ ミュータビリティの一つ目の特徴は、デフォルトでは無いという点です:
7
9
8
10
``` rust,ignore
9
11
let x = 5;
10
12
x = 6; // error!
11
13
```
12
14
13
- We can introduce mutability with the ` mut ` keyword:
15
+ <!-- We can introduce mutability with the `mut` keyword: -->
16
+ ` mut ` キーワードによりミュータビリティを導入できます:
14
17
15
18
``` rust
16
19
let mut x = 5 ;
17
20
18
21
x = 6 ; // no problem!
19
22
```
20
23
21
- This is a mutable [ variable binding] [ vb ] . When a binding is mutable, it means
22
- you’re allowed to change what the binding points to. So in the above example,
23
- it’s not so much that the value at ` x ` is changing, but that the binding
24
- changed from one ` i32 ` to another.
24
+ <!-- This is a mutable [variable binding][vb]. When a binding is mutable, it means -->
25
+ <!-- you’re allowed to change what the binding points to. So in the above example, -->
26
+ <!-- it’s not so much that the value at `x` is changing, but that the binding -->
27
+ <!-- changed from one `i32` to another. -->
28
+ これはミュータブルな [ 変数束縛] [ vb ] です。束縛がミュータブルのとき、束縛が何を指すかを変更して良いことを意味します。
29
+ つまり上記の例では、 ` x ` の値を変更したのではなく、束縛がある ` i32 ` から別のものへ変わったのです。
25
30
26
31
[ vb ] : variable-bindings.html
27
32
28
- If you want to change what the binding points to, you’ll need a [ mutable reference] [ mr ] :
33
+ <!-- If you want to change what the binding points to, you’ll need a [mutable reference][mr]: -->
34
+ 束縛先を変更したいのであれば、 [ ミュータブル参照] [ mr ] を使うことができます:
29
35
30
36
``` rust
31
37
let mut x = 5 ;
@@ -34,22 +40,28 @@ let y = &mut x;
34
40
35
41
[ mr ] : references-and-borrowing.html
36
42
37
- ` y ` is an immutable binding to a mutable reference, which means that you can’t
38
- bind ` y ` to something else (` y = &mut z ` ), but you can mutate the thing that’s
39
- bound to ` y ` (` *y = 5 ` ). A subtle distinction.
43
+ <!-- `y` is an immutable binding to a mutable reference, which means that you can’t -->
44
+ <!-- bind `y` to something else (`y = &mut z`), but you can mutate the thing that’s -->
45
+ <!-- bound to `y` (`*y = 5`). A subtle distinction. -->
46
+ ` y ` はミュータブル参照へのイミュータブル束縛であり、 ` y ` の束縛先を他のものに変える(` y = &mut z ` )ことはできません。
47
+ しかし、` y ` に束縛されているものを変化させること(` *y = 5 ` )は可能です。
40
48
41
- Of course, if you need both:
49
+ <!-- Of course, if you need both: -->
50
+ もちろん、両方が必要ならば:
42
51
43
52
``` rust
44
53
let mut x = 5 ;
45
54
let mut y = & mut x ;
46
55
```
47
56
48
- Now ` y ` can be bound to another value, and the value it’s referencing can be
49
- changed.
57
+ <!-- Now `y` can be bound to another value, and the value it’s referencing can be -->
58
+ <!-- changed. -->
59
+ 今度は ` y ` が他の値を束縛することもできますし、参照している値を変更することもできます。
50
60
51
- It’s important to note that ` mut ` is part of a [ pattern] [ pattern ] , so you
52
- can do things like this:
61
+ <!-- It’s important to note that `mut` is part of a [pattern][pattern], so you -->
62
+ <!-- can do things like this: -->
63
+ ` mut ` は [ パターン] [ pattern ] の一部であることに十分注意してください。
64
+ つまり、次のようなことを行えます:
53
65
54
66
``` rust
55
67
let (mut x , y ) = (5 , 6 );
@@ -60,11 +72,14 @@ fn foo(mut x: i32) {
60
72
61
73
[ pattern ] : patterns.html
62
74
63
- # Interior vs. Exterior Mutability
75
+ <!-- # Interior vs. Exterior Mutability -->
76
+ # 内側 vs. 外側のミュータビリティ
64
77
65
- However, when we say something is ‘immutable’ in Rust, that doesn’t mean that
66
- it’s not able to be changed: we mean something has ‘exterior mutability’. Consider,
67
- for example, [ ` Arc<T> ` ] [ arc ] :
78
+ <!-- However, when we say something is ‘immutable’ in Rust, that doesn’t mean that -->
79
+ <!-- it’s not able to be changed: we mean something has ‘exterior mutability’. Consider, -->
80
+ <!-- for example, [`Arc<T>`][arc]: -->
81
+ 一方で、Rustで「イミュータブル(immutable)」について言及するとき、変更不可能であることを意味しない:
82
+ 「外側のミュータビリティ(exterior mutability)」を表します。例として、 [ ` Arc<T> ` ] [ arc ] を考えます:
68
83
69
84
``` rust
70
85
use std :: sync :: Arc ;
@@ -75,30 +90,45 @@ let y = x.clone();
75
90
76
91
[ arc ] : ../std/sync/struct.Arc.html
77
92
78
- When we call ` clone() ` , the ` Arc<T> ` needs to update the reference count. Yet
79
- we’ve not used any ` mut ` s here, ` x ` is an immutable binding, and we didn’t take
80
- ` &mut 5 ` or anything. So what gives?
81
-
82
- To understand this, we have to go back to the core of Rust’s guiding
83
- philosophy, memory safety, and the mechanism by which Rust guarantees it, the
84
- [ ownership] [ ownership ] system, and more specifically, [ borrowing] [ borrowing ] :
85
-
86
- > You may have one or the other of these two kinds of borrows, but not both at
87
- > the same time:
93
+ <!-- When we call `clone()`, the `Arc<T>` needs to update the reference count. Yet -->
94
+ <!-- we’ve not used any `mut`s here, `x` is an immutable binding, and we didn’t take -->
95
+ <!-- `&mut 5` or anything. So what gives? -->
96
+ ` clone() ` を呼び出すとき、` Arc<T> ` は参照カウントを更新する必要があります。しかし、
97
+ ここでは ` mut ` を一切使っていません。つまり ` x ` はイミュータブル束縛であり、
98
+ ` &mut 5 ` のような引数もとりません。一体どうなっているの?
99
+
100
+ <!-- To understand this, we have to go back to the core of Rust’s guiding -->
101
+ <!-- philosophy, memory safety, and the mechanism by which Rust guarantees it, the -->
102
+ <!-- [ownership][ownership] system, and more specifically, [borrowing][borrowing]: -->
103
+ これを理解するには、Rust言語の設計哲学の中心をなすメモリ安全性、Rustがそれを保証するメカニズムである [ 所有権] [ ownership ] システム、
104
+ 特に [ 借用] [ borrowing ] に立ち返る必要があります。
105
+
106
+ <!-- > You may have one or the other of these two kinds of borrows, but not both at -->
107
+ <!-- > the same time: -->
108
+ <!-- > -->
109
+ <!-- > * one or more references (`&T`) to a resource, -->
110
+ <!-- > * exactly one mutable reference (`&mut T`). -->
111
+ > 次の2種類の借用のどちらか1つを持つことはありますが、両方を同時に持つことはありません。
88
112
>
89
- > * one or more references ( ` &T ` ) to a resource,
90
- > * exactly one mutable reference ( ` &mut T ` ).
113
+ > * リソースに対する1つ以上の参照( ` &T ` )
114
+ > * ただ1つのミュータブルな参照( ` &mut T ` )
91
115
92
116
[ ownership ] : ownership.html
93
117
[ borrowing ] : references-and-borrowing.html#borrowing
94
118
95
- So, that’s the real definition of ‘immutability’: is this safe to have two
96
- pointers to? In ` Arc<T> ` ’s case, yes: the mutation is entirely contained inside
97
- the structure itself. It’s not user facing. For this reason, it hands out ` &T `
98
- with ` clone() ` . If it handed out ` &mut T ` s, though, that would be a problem.
99
-
100
- Other types, like the ones in the [ ` std::cell ` ] [ stdcell ] module, have the
101
- opposite: interior mutability. For example:
119
+ <!-- So, that’s the real definition of ‘immutability’: is this safe to have two -->
120
+ <!-- pointers to? In `Arc<T>`’s case, yes: the mutation is entirely contained inside -->
121
+ <!-- the structure itself. It’s not user facing. For this reason, it hands out `&T` -->
122
+ <!-- with `clone()`. If it handed out `&mut T`s, though, that would be a problem. -->
123
+ つまり、「イミュータビリティ」の真の定義はこうです: これは2つから指されても安全か?
124
+ ` Arc<T> ` の例では、イエス: 変更は完全それ自身の構造の内側で行われます。ユーザ向いではありません。
125
+ このような理由により、 ` clone() ` を用いて ` T& ` を配るのです。仮に ` &mut T ` を配ってしまうと、
126
+ 問題になるでしょう。(訳注: ` Arc<T> ` を用いて複数スレッドへイミュータブルな値を配布する。)
127
+
128
+ <!-- Other types, like the ones in the [`std::cell`][stdcell] module, have the -->
129
+ <!-- opposite: interior mutability. For example: -->
130
+ [ ` std::cell ` ] [ stdcell ] モジュール中の他の型は、反対の性質: 内側のミュータビリティを持ちます。
131
+ 例えば:
102
132
103
133
``` rust
104
134
use std :: cell :: RefCell ;
0 commit comments