38
38
<!-- is _done at compile time_. You do not pay any run-time cost for any of these -->
39
39
<!-- features. -->
40
40
Rustは安全性とスピートに焦点を合わせます。
41
- Rustはそれらの目標をたくさんの「ゼロコスト抽象化」を通じて成し遂げます。それは、Rustでは抽象化を機能させるためのコストをできる限り小さくすることを意味します。
41
+ Rustはそれらの目標を、様々な「ゼロコスト抽象化」を通じて成し遂げます。
42
+ それは、Rustでは抽象化を機能させるためのコストをできる限り小さくすることを意味します。
42
43
所有権システムはゼロコスト抽象化の主な例です。
43
44
このガイドの中で話すであろう解析の全ては _ コンパイル時に行われます_ 。
44
45
それらのどの機能に対しても実行時のコストは全く掛かりません。
@@ -53,9 +54,11 @@ Rustはそれらの目標をたくさんの「ゼロコスト抽象化」を通
53
54
<!-- rules of the ownership system for a period of time, they fight the borrow -->
54
55
<!-- checker less and less. -->
55
56
しかし、このシステムはあるコストを持ちます。それは学習曲線です。
56
- 多くの新しいRustのユーザは「借用チェッカとの戦い」と好んで呼ばれるものを経験します。そこではRustコンパイラが開発者が正しいと考えるプログラムをコンパイルすることを拒絶します。
57
+ 多くのRust入門者は、私たちが「借用チェッカとの戦い」と呼ぶものを経験します。
58
+ そこではRustコンパイラが、開発者が正しいと考えるプログラムをコンパイルすることを拒絶します。
57
59
所有権がどのように機能するのかについてのプログラマのメンタルモデルがRustの実装する実際のルールにマッチしないため、これはしばしば起きます。
58
- しかし、よいニュースがあります。より経験豊富なRustの開発者は次のことを報告します。一度彼らが所有権システムのルールとともにしばらく仕事をすれば、彼らが借用チェッカと戦うことは少なくなっていくということです。
60
+ しかし、よいニュースがあります。より経験豊富なRustの開発者は次のことを報告します。
61
+ それは、所有権システムのルールと共にしばらく仕事をすれば、借用チェッカと戦うことは次第に少なくなっていく、というものです。
59
62
60
63
<!-- With that in mind, let’s learn about lifetimes. -->
61
64
それを念頭に置いて、所有権について学びましょう。
@@ -80,19 +83,19 @@ Rustはそれらの目標をたくさんの「ゼロコスト抽象化」を通
80
83
81
84
<!-- Uh oh! Your reference is pointing to an invalid resource. This is called a -->
82
85
<!-- dangling pointer or ‘use after free’, when the resource is memory. -->
83
- あー!
84
- あなたの参照は無効なリソースを指示しています 。
85
- リソースがメモリであるとき、これはダングリングポインタ又は 「解放後の使用」と呼ばれます。
86
+ あー!
87
+ あなたの参照は無効なリソースを指しています 。
88
+ リソースがメモリであるとき、これはダングリングポインタまたは 「解放後の使用」と呼ばれます。
86
89
87
90
<!-- To fix this, we have to make sure that step four never happens after step -->
88
91
<!-- three. The ownership system in Rust does this through a concept called -->
89
92
<!-- lifetimes, which describe the scope that a reference is valid for. -->
90
93
これを修正するためには、ステップ3の後にステップ4が絶対に起こらないようにしなければなりません。
91
94
Rustでの所有権システムはこれをライフタイムと呼ばれる概念を通じて行います。それは参照の有効なスコープを記述するものです。
92
95
93
- <!-- When we have a function that takes a reference by argument , we can be implicit -->
94
- <!-- or explicit about the lifetime of the reference: -->
95
- 引数として参照を受け取る関数について、参照のライフタイムを黙示又は明示することができます 。
96
+ <!-- When we have a function that takes an argument by reference , we can be -->
97
+ <!-- implicit or explicit about the lifetime of the reference: -->
98
+ 引数として参照を受け取る関数について、参照のライフタイムを黙示または明示できます 。
96
99
97
100
``` rust
98
101
# // implicit
@@ -124,10 +127,10 @@ fn bar<'a>(...)
124
127
<!-- discuss the `<>`s after a function’s name. A function can have ‘generic -->
125
128
<!-- parameters’ between the `<>`s, of which lifetimes are one kind. We’ll discuss -->
126
129
<!-- other kinds of generics [later in the book][generics], but for now, let’s -->
127
- <!-- just focus on the lifetimes aspect. -->
130
+ <!-- focus on the lifetimes aspect. -->
128
131
[ 関数の構文] [ functions ] については前に少し話しました。しかし、関数名の後の ` <> ` については議論しませんでした。
129
132
関数は ` <> ` の間に「ジェネリックパラメータ」を持つことができ、ライフタイムはその一種です。
130
- 他の種類のジェネリクスについては [ 本書の後の方] [ generics ] で議論しますが、とりあえず、ライフタイムの面だけに焦点を合わせましょう 。
133
+ 他の種類のジェネリクスについては [ 本書の後の方] [ generics ] で議論しますが、とりあえず、ライフタイムの面に焦点を合わせましょう 。
131
134
132
135
[ functions ] : functions.html
133
136
[ generics ] : generics.html
@@ -149,14 +152,14 @@ fn bar<'a, 'b>(...)
149
152
...(x: &'a i32)
150
153
```
151
154
152
- <!-- If we wanted an `&mut` reference, we’d do this: -->
155
+ <!-- If we wanted a `&mut` reference, we’d do this: -->
153
156
もし ` &mut ` 参照が欲しいのならば、次のようにします。
154
157
155
158
``` rust,ignore
156
159
...(x: &'a mut i32)
157
160
```
158
161
159
- <!-- If you compare `&mut i32` to `&'a mut i32`, they’re the same, it’s just that -->
162
+ <!-- If you compare `&mut i32` to `&'a mut i32`, they’re the same, it’s that -->
160
163
<!-- the lifetime `'a` has snuck in between the `&` and the `mut i32`. We read `&mut -->
161
164
<!-- i32` as ‘a mutable reference to an `i32`’ and `&'a mut i32` as ‘a mutable -->
162
165
<!-- reference to an `i32` with the lifetime `'a`’. -->
@@ -187,7 +190,7 @@ fn main() {
187
190
[ structs ] : structs.html
188
191
189
192
<!-- As you can see, `struct`s can also have lifetimes. In a similar way to functions, -->
190
- 見てのとおり、 ` struct ` もライフタイムを持つことができます 。
193
+ 見てのとおり、 ` struct ` もライフタイムを持てます 。
191
194
これは関数と同じ方法です。
192
195
193
196
``` rust
@@ -236,16 +239,16 @@ fn main() {
236
239
```
237
240
238
241
<!-- As you can see, we need to declare a lifetime for `Foo` in the `impl` line. We repeat -->
239
- <!-- `'a` twice, just like on functions: `impl<'a>` defines a lifetime `'a`, and `Foo<'a>` -->
242
+ <!-- `'a` twice, like on functions: `impl<'a>` defines a lifetime `'a`, and `Foo<'a>` -->
240
243
<!-- uses it. -->
241
244
見てのとおり、 ` Foo ` のライフタイムは ` impl ` 行で宣言する必要があります。
242
- ちょうど関数のときのように ` 'a ` は2回繰り返されます。つまり、 ` impl<'a> ` はライフタイム ` 'a ` を定義し、 ` Foo<'a> ` はそれを使うのです。
245
+ 関数のときのように ` 'a ` は2回繰り返されます。つまり、 ` impl<'a> ` はライフタイム ` 'a ` を定義し、 ` Foo<'a> ` はそれを使うのです。
243
246
244
247
<!-- ## Multiple lifetimes -->
245
248
## 複数のライフタイム
246
249
247
250
<!-- If you have multiple references, you can use the same lifetime multiple times: -->
248
- もし複数の参照があれば、同じライフタイムを複数回使うことができます 。
251
+ もし複数の参照があるなら、同じライフタイムを何度でも使えます 。
249
252
250
253
``` rust
251
254
fn x_or_y <'a >(x : & 'a str , y : & 'a str ) -> & 'a str {
@@ -257,7 +260,7 @@ fn x_or_y<'a>(x: &'a str, y: &'a str) -> &'a str {
257
260
<!-- return value is also alive for that scope. If you wanted `x` and `y` to have -->
258
261
<!-- different lifetimes, you can use multiple lifetime parameters: -->
259
262
これは ` x ` と ` y ` が両方とも同じスコープで有効であり、戻り値もそのスコープで有効であることを示します。
260
- もし ` x ` と ` y ` に違うライフタイムを持たせたいのであれば、複数のライフタイムパラメータを使うことができます 。
263
+ もし ` x ` と ` y ` に違うライフタイムを持たせたいのであれば、複数のライフタイムパラメータを使えます 。
261
264
262
265
``` rust
263
266
fn x_or_y <'a , 'b >(x : & 'a str , y : & 'b str ) -> & 'a str {
@@ -349,14 +352,14 @@ fn main() {
349
352
<!-- Whew! As you can see here, the scopes of `f` and `y` are smaller than the scope -->
350
353
<!-- of `x`. But when we do `x = &f.x`, we make `x` a reference to something that’s -->
351
354
<!-- about to go out of scope. -->
352
- ふう!
355
+ ふう!
353
356
見てのとおり、ここでは ` f ` と ` y ` のスコープは ` x ` のスコープよりも小さいです。
354
357
しかし ` x = &f.x ` を実行するとき、 ` x ` をまさにスコープから外れた何かの参照にしてしまいます。
355
358
356
359
<!-- Named lifetimes are a way of giving these scopes a name. Giving something a -->
357
360
<!-- name is the first step towards being able to talk about it. -->
358
361
名前の付いたライフタイムはそれらのスコープに名前を与える方法です。
359
- 何かに名前を与えることはそれについて話をすることができるようになるための最初のステップです 。
362
+ 何かに名前を与えることはそれについて話をできるようになるための最初のステップです 。
360
363
361
364
<!-- ## 'static -->
362
365
## 'static
@@ -390,19 +393,18 @@ let x: &'static i32 = &FOO;
390
393
<!-- ## Lifetime Elision -->
391
394
## ライフタイムの省略
392
395
393
- <!-- Rust supports powerful local type inference in function bodies, but it’s -->
394
- <!-- forbidden in item signatures to allow reasoning about the types based on -->
395
- <!-- the item signature alone. However, for ergonomic reasons a very restricted -->
396
- <!-- secondary inference algorithm called “lifetime elision” applies in function -->
397
- <!-- signatures. It infers only based on the signature components themselves and not -->
398
- <!-- based on the body of the function, only infers lifetime parameters, and does -->
399
- <!-- this with only three easily memorizable and unambiguous rules. This makes -->
400
- <!-- lifetime elision a shorthand for writing an item signature, while not hiding -->
396
+ <!-- Rust supports powerful local type inference in the bodies of functions but not in their item signatures. -->
397
+ <!-- It's forbidden to allow reasoning about types based on the item signature alone. -->
398
+ <!-- However, for ergonomic reasons, a very restricted secondary inference algorithm called -->
399
+ <!-- “lifetime elision” does apply when judging lifetimes. Lifetime elision is concerned solely to infer -->
400
+ <!-- lifetime parameters using three easily memorizable and unambiguous rules. This means lifetime elision -->
401
+ <!-- acts as a shorthand for writing an item signature, while not hiding -->
401
402
<!-- away the actual types involved as full local inference would if applied to it. -->
402
- Rustは関数本体の部分では強力なローカル型推論をサポートします。しかし要素のシグネチャの部分では、型が要素のシグネチャだけでわかるようにするため、(型推論が)許されていません。
403
- とはいえ、エルゴノミック(人間にとっての扱いやすさ)の理由により、"ライフタイムの省略"と呼ばれている、非常に制限された第二の推論アルゴリズムがシグネチャの部分に適用されます。
404
- その推論はシグネチャのコンポーネントだけに基づき、関数本体には基づかず、ライフタイムパラメータだけを推論します。そしてたった3つの覚えやすく明確なルールに従って行います。
405
- ライフタイムの省略で要素のシグネチャを短く書くことができます。しかしローカル型推論が適用されるときのように実際の型を隠すことはできません。
403
+ Rustは関数本体については強力なローカル型推論をサポートしますが、要素のシグネチャについては別です。
404
+ そこで型推論が許されていないのは、要素のシグネチャだけで型がわかるようにするためです。
405
+ とはいえ、エルゴノミック(人間にとっての扱いやすさ)上の理由により、ライフタイムを決定する際には、「ライフタイムの省略」と呼ばれる、非常に制限された第二の推論アルゴリズムが適用されます。
406
+ ライフタイムの推論は、ライフタイムパラメータの推論だけに関係しており、たった3つの覚えやすく明確なルールに従います。
407
+ ライフタイムの省略は要素のシグネチャを短く書けることを意味しますが、ローカル型推論が適用されるときのように実際の型を隠すことはできません。
406
408
407
409
<!-- When talking about lifetime elision, we use the term *input lifetime* and -->
408
410
<!-- *output lifetime*. An *input lifetime* is a lifetime associated with a parameter -->
@@ -431,19 +433,19 @@ fn foo<'a>(bar: &'a str) -> &'a str
431
433
```
432
434
433
435
<!-- Here are the three rules: -->
434
- これが3つのルールです 。
436
+ 3つのルールを以下に示します 。
435
437
436
438
<!-- * Each elided lifetime in a function’s arguments becomes a distinct lifetime -->
437
439
<!-- parameter. -->
438
- * 関数の引数の中の省略された各ライフタイムは互いに異なるライフタイムパラメータになる
440
+ * 関数の引数の中の省略された各ライフタイムは、互いに異なるライフタイムパラメータになる
439
441
440
442
<!-- * If there is exactly one input lifetime, elided or not, that lifetime is -->
441
443
<!-- assigned to all elided lifetimes in the return values of that function. -->
442
444
* もし入力ライフタイムが1つだけならば、省略されたかどうかにかかわらず、そのライフタイムはその関数の戻り値の中の省略されたライフタイム全てに割り当てられる
443
445
444
446
<!-- * If there are multiple input lifetimes, but one of them is `&self` or `&mut -->
445
447
<!-- self`, the lifetime of `self` is assigned to all elided output lifetimes. -->
446
- * もし入力ライフタイムが複数あるが、その1つが ` &self ` 又は ` &mut self ` であれば、 ` self ` のライフタイムは省略された出力ライフタイム全てに割り当てられる
448
+ * もし入力ライフタイムが複数あるが、その1つが ` &self ` または ` &mut self ` であれば、 ` self ` のライフタイムは省略された出力ライフタイム全てに割り当てられる
447
449
448
450
<!-- Otherwise, it is an error to elide an output lifetime. -->
449
451
そうでないときは、出力ライフタイムの省略はエラーです。
@@ -492,10 +494,10 @@ fn frob<'a, 'b>(s: &'a str, t: &'b str) -> &str; // 展開された形。出力
492
494
fn get_mut(&mut self) -> &mut T; // 省略された形
493
495
fn get_mut<'a>(&'a mut self) -> &'a mut T; // 展開された形
494
496
495
- # // fn args<T:ToCStr>(&mut self, args: &[T]) -> &mut Command; // elided
496
- # // fn args<'a, 'b, T:ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command; // expanded
497
- fn args<T:ToCStr>(&mut self, args: &[T]) -> &mut Command; // 省略された形
498
- fn args<'a, 'b, T:ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command; // 展開された形
497
+ # // fn args<T: ToCStr>(&mut self, args: &[T]) -> &mut Command; // elided
498
+ # // fn args<'a, 'b, T: ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command; // expanded
499
+ fn args<T: ToCStr>(&mut self, args: &[T]) -> &mut Command; // 省略された形
500
+ fn args<'a, 'b, T: ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command; // 展開された形
499
501
500
502
# // fn new(buf: &mut [u8]) -> BufWriter; // elided
501
503
# // fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a>; // expanded
0 commit comments