Skip to content

Commit efd044a

Browse files
authored
Merge pull request #166 from tatsuya6502/references-and-borrowing-1.9
4.9. References and Borrowing (1.9)
2 parents d3abd63 + e55581d commit efd044a

File tree

2 files changed

+27
-95
lines changed

2 files changed

+27
-95
lines changed

1.9/ja/book/references-and-borrowing.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
<!-- Rust has a focus on safety and speed. It accomplishes these goals through many -->
3636
<!-- ‘zero-cost abstractions’, which means that in Rust, abstractions cost as little -->
3737
<!-- 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 -->
3939
<!-- is _done at compile time_. You do not pay any run-time cost for any of these -->
4040
<!-- features. -->
4141
Rustは安全性とスピートに焦点を合わせます。
42-
Rustはそれらの目標をたくさんの「ゼロコスト抽象化」を通じて成し遂げます。それは、Rustでは抽象化を機能させるためのコストをできる限り小さくすることを意味します。
42+
Rustはそれらの目標を、様々な「ゼロコスト抽象化」を通じて成し遂げます。
43+
それは、Rustでは抽象化を機能させるためのコストをできる限り小さくすることを意味します。
4344
所有権システムはゼロコスト抽象化の主な例です。
4445
このガイドの中で話すであろう解析の全ては _コンパイル時に行われます_
4546
それらのどの機能に対しても実行時のコストは全く掛かりません。
@@ -54,9 +55,11 @@ Rustはそれらの目標をたくさんの「ゼロコスト抽象化」を通
5455
<!-- rules of the ownership system for a period of time, they fight the borrow -->
5556
<!-- checker less and less. -->
5657
しかし、このシステムはあるコストを持ちます。それは学習曲線です。
57-
多くの新しいRustのユーザは「借用チェッカとの戦い」と好んで呼ばれるものを経験します。そこではRustコンパイラが開発者が正しいと考えるプログラムをコンパイルすることを拒絶します。
58+
多くのRust入門者は、私たちが「借用チェッカとの戦い」と呼ぶものを経験します。
59+
そこではRustコンパイラが、開発者が正しいと考えるプログラムをコンパイルすることを拒絶します。
5860
所有権がどのように機能するのかについてのプログラマのメンタルモデルがRustの実装する実際のルールにマッチしないため、これはしばしば起きます。
59-
しかし、よいニュースがあります。より経験豊富なRustの開発者は次のことを報告します。一度彼らが所有権システムのルールとともにしばらく仕事をすれば、彼らが借用チェッカと戦うことは少なくなっていくということです。
61+
しかし、よいニュースがあります。より経験豊富なRustの開発者は次のことを報告します。
62+
それは、所有権システムのルールと共にしばらく仕事をすれば、借用チェッカと戦うことは次第に少なくなっていく、というものです。
6063

6164
<!-- With that in mind, let’s learn about borrowing. -->
6265
それを念頭に置いて、借用について学びましょう。
@@ -120,9 +123,9 @@ let answer = foo(&v1, &v2);
120123
何かを借用した束縛はそれがスコープから外れるときにリソースを割当解除しません。
121124
これは `foo()` の呼出しの後に元の束縛を再び使うことができることを意味します。
122125

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()`, -->
124127
<!-- the vectors can’t be changed at all: -->
125-
参照は束縛とちょうど同じようにイミュータブルです
128+
参照は束縛と同じようにイミュータブルです
126129
これは `foo()` の中ではベクタは全く変更できないことを意味します。
127130

128131
```rust,ignore
@@ -153,7 +156,7 @@ v.push(5);
153156
<!-- There’s a second kind of reference: `&mut T`. A ‘mutable reference’ allows you -->
154157
<!-- to mutate the resource you’re borrowing. For example: -->
155158
参照には2つ目の種類、 `&mut T` があります。
156-
「ミュータブルな参照」によって借用しているリソースを変更することができるようになります
159+
「ミュータブルな参照」によって借用しているリソースを変更できるようになります
157160
例は次のとおりです。
158161

159162
```rust
@@ -168,22 +171,22 @@ println!("{}", x);
168171
<!-- This will print `6`. We make `y` a mutable reference to `x`, then add one to -->
169172
<!-- the thing `y` points at. You’ll notice that `x` had to be marked `mut` as well. -->
170173
<!-- If it wasn’t, we couldn’t take a mutable borrow to an immutable value. -->
171-
これは `6` をプリントするでしょう
174+
これは `6` を表示するでしょう
172175
`y``x` へのミュータブルな参照にして、それから `y` の指示先に1を足します。
173176
`x``mut` とマークしなければならないことに気付くでしょう。
174177
そうしないと、イミュータブルな値へのミュータブルな借用ということになってしまい、使うことができなくなってしまいます。
175178

176179
<!-- 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 -->
178181
<!-- accessing the contents of a reference as well. -->
179182
アスタリスク( `*` )を `y` の前に追加して、それを `*y` にしたことにも気付くでしょう。これは、 `y``&mut` 参照だからです。
180183
参照の内容にアクセスするためにもそれらを使う必要があるでしょう。
181184

182-
<!-- Otherwise, `&mut` references are just like references. There _is_ a large -->
185+
<!-- Otherwise, `&mut` references are like references. There _is_ a large -->
183186
<!-- difference between the two, and how they interact, though. You can tell -->
184187
<!-- something is fishy in the above example, because we need that extra scope, with -->
185188
<!-- the `{` and `}`. If we remove them, we get an error: -->
186-
それ以外は、 `&mut` 参照は普通の参照と全く同じです
189+
それ以外は、 `&mut` 参照は普通の参照と同じです
187190
しかし、2つの間には、そしてそれらがどのように相互作用するかには大きな違いが _あります_
188191
前の例で何かが怪しいと思ったかもしれません。なぜなら、 `{``}` を使って追加のスコープを必要とするからです。
189192
もしそれらを削除すれば、次のようなエラーが出ます。
@@ -223,8 +226,8 @@ fn main() {
223226
* リソースに対する1つ以上の参照( `&T`
224227
* ただ1つのミュータブルな参照( `&mut T`
225228

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: -->
228231
これがデータ競合の定義と非常に似ていることに気付くかもしれません。全く同じではありませんが。
229232

230233
<!-- &lt; There is a ‘data race’ when two or more pointers access the same memory -->
@@ -283,11 +286,13 @@ fn main() {
283286
```
284287

285288
<!-- 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!` にイミュータブルな借用を提供できるわけです。
291296
Rustでは借用はその有効なスコープと結び付けられます。
292297
そしてスコープはこのように見えます。
293298

@@ -331,9 +336,9 @@ println!("{}", x); // <- ここでxを借用しようとする
331336

332337
<!-- There’s no problem. Our mutable borrow goes out of scope before we create an -->
333338
<!-- immutable one. But scope is the key to seeing how long a borrow lasts for. -->
334-
問題ありません
339+
これなら問題ありません
335340
ミュータブルな借用はイミュータブルな借用を作る前にスコープから外れます。
336-
しかしスコープは借用がどれくらい存続するのか理解するための鍵となります
341+
しかしスコープは、借用がどれくらい存続するのか理解するための鍵となります
337342

338343
<!-- ## Issues borrowing prevents -->
339344
## 借用が回避する問題
@@ -362,10 +367,10 @@ for i in &v {
362367
}
363368
```
364369

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 -->
366371
<!-- only given references to the elements. And `v` is itself borrowed as immutable, -->
367372
<!-- which means we can’t change it while we’re iterating: -->
368-
これは1から3までをプリントアウトします
373+
これは1から3までを表示します
369374
ベクタに対して繰り返すとき、要素への参照だけを受け取ります。
370375
そして、 `v` はそれ自体イミュータブルとして借用され、それは繰返しを行っている間はそれを変更できないことを意味します。
371376

diff-1.6.0..1.9.0/src/doc/book/references-and-borrowing.md

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)