Skip to content

Commit 76cc246

Browse files
committed
WIP: translating mutability.md
1 parent 55bad82 commit 76cc246

File tree

1 file changed

+70
-40
lines changed

1 file changed

+70
-40
lines changed

1.6/ja/book/mutability.md

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
% ミュータビリティ
22
<!-- % Mutability -->
33

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+
ミュータビリティの一つ目の特徴は、デフォルトでは無いという点です:
79

810
```rust,ignore
911
let x = 5;
1012
x = 6; // error!
1113
```
1214

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

1518
```rust
1619
let mut x = 5;
1720

1821
x = 6; // no problem!
1922
```
2023

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` から別のものへ変わったのです。
2530

2631
[vb]: variable-bindings.html
2732

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] を使うことができます:
2935

3036
```rust
3137
let mut x = 5;
@@ -34,22 +40,28 @@ let y = &mut x;
3440

3541
[mr]: references-and-borrowing.html
3642

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`)は可能です。
4048

41-
Of course, if you need both:
49+
<!-- Of course, if you need both: -->
50+
もちろん、両方が必要ならば:
4251

4352
```rust
4453
let mut x = 5;
4554
let mut y = &mut x;
4655
```
4756

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` が他の値を束縛することもできますし、参照している値を変更することもできます。
5060

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+
つまり、次のようなことを行えます:
5365

5466
```rust
5567
let (mut x, y) = (5, 6);
@@ -60,11 +72,14 @@ fn foo(mut x: i32) {
6072

6173
[pattern]: patterns.html
6274

63-
# Interior vs. Exterior Mutability
75+
<!-- # Interior vs. Exterior Mutability -->
76+
# 内側 vs. 外側のミュータビリティ
6477

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] を考えます:
6883

6984
```rust
7085
use std::sync::Arc;
@@ -75,30 +90,45 @@ let y = x.clone();
7590

7691
[arc]: ../std/sync/struct.Arc.html
7792

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つを持つことはありますが、両方を同時に持つことはありません。
88112
>
89-
> * one or more references (`&T`) to a resource,
90-
> * exactly one mutable reference (`&mut T`).
113+
> * リソースに対する1つ以上の参照(`&T`
114+
> * ただ1つのミュータブルな参照(`&mut T`
91115
92116
[ownership]: ownership.html
93117
[borrowing]: references-and-borrowing.html#borrowing
94118

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+
例えば:
102132

103133
```rust
104134
use std::cell::RefCell;

0 commit comments

Comments
 (0)