35
35
<!-- Rust has a focus on safety and speed. It accomplishes these goals through many -->
36
36
<!-- ‘zero-cost abstractions’, which means that in Rust, abstractions cost as little -->
37
37
<!-- as possible in order to make them work. The ownership system is a prime example -->
38
- <!-- of a zero cost abstraction. All of the analysis we’ll talk about in this guide -->
38
+ <!-- of a zero- cost abstraction. All of the analysis we’ll talk about in this guide -->
39
39
<!-- is _done at compile time_. You do not pay any run-time cost for any of these -->
40
40
<!-- features. -->
41
41
Rustは安全性とスピートに焦点を合わせます。
42
- Rustはそれらの目標をたくさんの「ゼロコスト抽象化」を通じて成し遂げます。それは、Rustでは抽象化を機能させるためのコストをできる限り小さくすることを意味します。
42
+ Rustはそれらの目標を、様々な「ゼロコスト抽象化」を通じて成し遂げます。
43
+ それは、Rustでは抽象化を機能させるためのコストをできる限り小さくすることを意味します。
43
44
所有権システムはゼロコスト抽象化の主な例です。
44
45
このガイドの中で話すであろう解析の全ては _ コンパイル時に行われます_ 。
45
46
それらのどの機能に対しても実行時のコストは全く掛かりません。
@@ -54,9 +55,11 @@ Rustはそれらの目標をたくさんの「ゼロコスト抽象化」を通
54
55
<!-- rules of the ownership system for a period of time, they fight the borrow -->
55
56
<!-- checker less and less. -->
56
57
しかし、このシステムはあるコストを持ちます。それは学習曲線です。
57
- 多くの新しいRustのユーザは「借用チェッカとの戦い」と好んで呼ばれるものを経験します。そこではRustコンパイラが開発者が正しいと考えるプログラムをコンパイルすることを拒絶します。
58
+ 多くのRust入門者は、私たちが「借用チェッカとの戦い」と呼ぶものを経験します。
59
+ そこではRustコンパイラが、開発者が正しいと考えるプログラムをコンパイルすることを拒絶します。
58
60
所有権がどのように機能するのかについてのプログラマのメンタルモデルがRustの実装する実際のルールにマッチしないため、これはしばしば起きます。
59
- しかし、よいニュースがあります。より経験豊富なRustの開発者は次のことを報告します。一度彼らが所有権システムのルールとともにしばらく仕事をすれば、彼らが借用チェッカと戦うことは少なくなっていくということです。
61
+ しかし、よいニュースがあります。より経験豊富なRustの開発者は次のことを報告します。
62
+ それは、所有権システムのルールと共にしばらく仕事をすれば、借用チェッカと戦うことは次第に少なくなっていく、というものです。
60
63
61
64
<!-- With that in mind, let’s learn about borrowing. -->
62
65
それを念頭に置いて、借用について学びましょう。
@@ -120,9 +123,9 @@ let answer = foo(&v1, &v2);
120
123
何かを借用した束縛はそれがスコープから外れるときにリソースを割当解除しません。
121
124
これは ` foo() ` の呼出しの後に元の束縛を再び使うことができることを意味します。
122
125
123
- <!-- References are immutable, just like bindings. This means that inside of `foo()`, -->
126
+ <!-- References are immutable, like bindings. This means that inside of `foo()`, -->
124
127
<!-- the vectors can’t be changed at all: -->
125
- 参照は束縛とちょうど同じようにイミュータブルです 。
128
+ 参照は束縛と同じようにイミュータブルです 。
126
129
これは ` foo() ` の中ではベクタは全く変更できないことを意味します。
127
130
128
131
``` rust,ignore
@@ -153,7 +156,7 @@ v.push(5);
153
156
<!-- There’s a second kind of reference: `&mut T`. A ‘mutable reference’ allows you -->
154
157
<!-- to mutate the resource you’re borrowing. For example: -->
155
158
参照には2つ目の種類、 ` &mut T ` があります。
156
- 「ミュータブルな参照」によって借用しているリソースを変更することができるようになります 。
159
+ 「ミュータブルな参照」によって借用しているリソースを変更できるようになります 。
157
160
例は次のとおりです。
158
161
159
162
``` rust
@@ -168,22 +171,22 @@ println!("{}", x);
168
171
<!-- This will print `6`. We make `y` a mutable reference to `x`, then add one to -->
169
172
<!-- the thing `y` points at. You’ll notice that `x` had to be marked `mut` as well. -->
170
173
<!-- If it wasn’t, we couldn’t take a mutable borrow to an immutable value. -->
171
- これは ` 6 ` をプリントするでしょう 。
174
+ これは ` 6 ` を表示するでしょう 。
172
175
` y ` を ` x ` へのミュータブルな参照にして、それから ` y ` の指示先に1を足します。
173
176
` x ` も ` mut ` とマークしなければならないことに気付くでしょう。
174
177
そうしないと、イミュータブルな値へのミュータブルな借用ということになってしまい、使うことができなくなってしまいます。
175
178
176
179
<!-- You'll also notice we added an asterisk (`*`) in front of `y`, making it `*y`, -->
177
- <!-- this is because `y` is an `&mut` reference. You'll also need to use them for -->
180
+ <!-- this is because `y` is a `&mut` reference. You'll also need to use them for -->
178
181
<!-- accessing the contents of a reference as well. -->
179
182
アスタリスク( ` * ` )を ` y ` の前に追加して、それを ` *y ` にしたことにも気付くでしょう。これは、 ` y ` が ` &mut ` 参照だからです。
180
183
参照の内容にアクセスするためにもそれらを使う必要があるでしょう。
181
184
182
- <!-- Otherwise, `&mut` references are just like references. There _is_ a large -->
185
+ <!-- Otherwise, `&mut` references are like references. There _is_ a large -->
183
186
<!-- difference between the two, and how they interact, though. You can tell -->
184
187
<!-- something is fishy in the above example, because we need that extra scope, with -->
185
188
<!-- the `{` and `}`. If we remove them, we get an error: -->
186
- それ以外は、 ` &mut ` 参照は普通の参照と全く同じです 。
189
+ それ以外は、 ` &mut ` 参照は普通の参照と同じです 。
187
190
しかし、2つの間には、そしてそれらがどのように相互作用するかには大きな違いが _ あります_ 。
188
191
前の例で何かが怪しいと思ったかもしれません。なぜなら、 ` { ` と ` } ` を使って追加のスコープを必要とするからです。
189
192
もしそれらを削除すれば、次のようなエラーが出ます。
@@ -223,8 +226,8 @@ fn main() {
223
226
* リソースに対する1つ以上の参照( ` &T ` )
224
227
* ただ1つのミュータブルな参照( ` &mut T ` )
225
228
226
- <!-- You may notice that this is very similar, though not exactly the same as, -->
227
- <!-- to the definition of a data race: -->
229
+ <!-- You may notice that this is very similar to , though not exactly the same as, -->
230
+ <!-- the definition of a data race: -->
228
231
これがデータ競合の定義と非常に似ていることに気付くかもしれません。全く同じではありませんが。
229
232
230
233
<!-- < There is a ‘data race’ when two or more pointers access the same memory -->
@@ -283,11 +286,13 @@ fn main() {
283
286
```
284
287
285
288
<!-- In other words, the mutable borrow is held through the rest of our example. What -->
286
- <!-- we want is for the mutable borrow to end _before_ we try to call `println!` and -->
287
- <!-- make an immutable borrow. In Rust, borrowing is tied to the scope that the -->
288
- <!-- borrow is valid for. And our scopes look like this: -->
289
- 言い換えると、ミュータブルな借用は先程の例の残りの間ずっと保持されるということです。
290
- 必要なものは、 ` println! ` を呼び出し、イミュータブルな借用を作ろうとする _ 前に_ 終わるミュータブルな借用です。
289
+ <!-- we want is for the mutable borrow by `y` to end so that the resource can be -->
290
+ <!-- returned to the owner, `x`. `x` can then provide a immutable borrow to `println!`. -->
291
+ <!-- In Rust, borrowing is tied to the scope that the borrow is valid for. And our -->
292
+ <!-- scopes look like this: -->
293
+ 言い換えると、ミュータブルな借用は、先ほどの例の残りの間、ずっと保持されるということです。
294
+ ここで私たちが求めているのは、` y ` によるミュータブルな借用が終わり、リソースがその所有者である ` x ` に返却されることです。
295
+ そうすれば ` x ` は ` println! ` にイミュータブルな借用を提供できるわけです。
291
296
Rustでは借用はその有効なスコープと結び付けられます。
292
297
そしてスコープはこのように見えます。
293
298
@@ -331,9 +336,9 @@ println!("{}", x); // <- ここでxを借用しようとする
331
336
332
337
<!-- There’s no problem. Our mutable borrow goes out of scope before we create an -->
333
338
<!-- immutable one. But scope is the key to seeing how long a borrow lasts for. -->
334
- 問題ありません 。
339
+ これなら問題ありません 。
335
340
ミュータブルな借用はイミュータブルな借用を作る前にスコープから外れます。
336
- しかしスコープは借用がどれくらい存続するのか理解するための鍵となります 。
341
+ しかしスコープは、借用がどれくらい存続するのか理解するための鍵となります 。
337
342
338
343
<!-- ## Issues borrowing prevents -->
339
344
## 借用が回避する問題
@@ -362,10 +367,10 @@ for i in &v {
362
367
}
363
368
```
364
369
365
- <!-- This prints out one through three. As we iterate through the vectors , we’re -->
370
+ <!-- This prints out one through three. As we iterate through the vector , we’re -->
366
371
<!-- only given references to the elements. And `v` is itself borrowed as immutable, -->
367
372
<!-- which means we can’t change it while we’re iterating: -->
368
- これは1から3までをプリントアウトします 。
373
+ これは1から3までを表示します 。
369
374
ベクタに対して繰り返すとき、要素への参照だけを受け取ります。
370
375
そして、 ` v ` はそれ自体イミュータブルとして借用され、それは繰返しを行っている間はそれを変更できないことを意味します。
371
376
0 commit comments