5
5
## ライフタイムで参照を検証する
6
6
7
7
<!--
8
- One detail we didn’t discuss in the “References and Borrowing” section in
9
- Chapter 4 is that every reference in Rust has a *lifetime*, which is the scope
10
- for which that reference is valid. Most of the time, lifetimes are implicit and
11
- inferred, just like most of the time, types are inferred. We must annotate types
12
- when multiple types are possible. In a similar way, we must annotate lifetimes
13
- when the lifetimes of references could be related in a few different ways. Rust
14
- requires us to annotate the relationships using generic lifetime parameters to
15
- ensure the actual references used at runtime will definitely be valid.
8
+ One detail we didn’t discuss in the [“References and
9
+ Borrowing”][references-and-borrowing] section in Chapter 4 is
10
+ that every reference in Rust has a *lifetime*, which is the scope for which
11
+ that reference is valid. Most of the time, lifetimes are implicit and
12
+ inferred, just like most of the time, types are inferred. We must annotate
13
+ types when multiple types are possible. In a similar way, we must annotate
14
+ lifetimes when the lifetimes of references could be related in a few different
15
+ ways. Rust requires us to annotate the relationships using generic lifetime
16
+ parameters to ensure the actual references used at runtime will definitely be
17
+ valid.
16
18
-->
17
19
18
20
第4章の「参照と借用」節で議論しなかった詳細の一つに、Rustにおいて参照は全てライフタイムを保持するということがあります。
@@ -30,8 +32,7 @@ The concept of lifetimes is somewhat different from tools in other programming
30
32
languages, arguably making lifetimes Rust’s most distinctive feature. Although
31
33
we won’t cover lifetimes in their entirety in this chapter, we’ll discuss
32
34
common ways you might encounter lifetime syntax so you can become familiar with
33
- the concepts. See the “Advanced Lifetimes” section in Chapter 19 for more
34
- detailed information.
35
+ the concepts.
35
36
-->
36
37
37
38
ライフタイムの概念は、他のプログラミング言語の道具とはどこか異なり、間違いなく、
@@ -77,9 +78,9 @@ has gone out of scope</span>
77
78
<span class =" caption " >リスト10-17: 値がスコープを抜けてしまった参照を使用しようとする</span >
78
79
79
80
<!--
80
- > Note: The example in Listing 10-17, 10-18, and 10-24 declare variables
81
+ > Note: The examples in Listings 10-17, 10-18, and 10-24 declare variables
81
82
> without giving them an initial value, so the variable name exists in the
82
- > outer scope. At first glance, this might appear to be in conflict with Rust' s
83
+ > outer scope. At first glance, this might appear to be in conflict with Rust’ s
83
84
> having no null values. However, if we try to use a variable before giving it
84
85
> a value, we’ll get a compile-time error, which shows that Rust indeed does
85
86
> not allow null values.
@@ -146,7 +147,7 @@ Rustで、このコードが動くことを許可していたら、`r`は`x`が
146
147
<!--
147
148
The Rust compiler has a *borrow checker* that compares scopes to determine
148
149
whether all borrows are valid. Listing 10-18 shows the same code as Listing
149
- 10-17 but with annotations showing the lifetimes of the variables:
150
+ 10-17 but with annotations showing the lifetimes of the variables.
150
151
-->
151
152
152
153
Rustコンパイラには、スコープを比較して全ての借用が有効であるかを決定する* 借用チェッカー* があります。
@@ -274,21 +275,15 @@ function to find the longer of two string slices</span>
274
275
<!--
275
276
Note that we want the function to take string slices, which are references,
276
277
because we don’t want the `longest` function to take ownership of its
277
- parameters. We want to allow the function to accept slices of a `String` (the
278
- type stored in the variable `string1`) as well as string literals (which is
279
- what variable `string2` contains).
278
+ parameters. Refer to the [“String Slices as
279
+ Parameters”][string-slices-as-parameters] section in Chapter 4
280
+ for more discussion about why the parameters we use in Listing 10-20 are the
281
+ ones we want.
280
282
-->
281
283
282
284
関数に取ってほしい引数が文字列スライス、つまり参照であることに注意してください。
283
285
何故なら、` longest ` 関数に引数の所有権を奪ってほしくないからです。
284
286
この関数に` String ` のスライス(変数` string1 ` に格納されている型)と文字列リテラル(変数` string2 ` が含むもの)を受け取らせたいのです。
285
-
286
- <!--
287
- Refer to the “String Slices as Parameters” section in Chapter 4 for more
288
- discussion about why the parameters we use in Listing 10-20 are the ones we
289
- want.
290
- -->
291
-
292
287
リスト10-20で使用している引数が、我々が必要としているものである理由についてもっと詳しい議論は、
293
288
第4章の「引数としての文字列スライス」節をご参照ください。
294
289
@@ -500,13 +495,15 @@ The function signature now tells Rust that for some lifetime `'a`, the function
500
495
takes two parameters, both of which are string slices that live at least as
501
496
long as lifetime `'a`. The function signature also tells Rust that the string
502
497
slice returned from the function will live at least as long as lifetime `'a`.
503
- These constraints are what we want Rust to enforce. Remember, when we specify
504
- the lifetime parameters in this function signature, we’re not changing the
505
- lifetimes of any values passed in or returned. Rather, we’re specifying that
506
- the borrow checker should reject any values that don’t adhere to these
507
- constraints. Note that the `longest` function doesn’t need to know exactly how
508
- long `x` and `y` will live, only that some scope can be substituted for `'a`
509
- that will satisfy this signature.
498
+ In practice, it means that the lifetime of the reference returned by the
499
+ `longest` function is the same as the smaller of the lifetimes of the
500
+ references passed in. These constraints are what we want Rust to enforce.
501
+ Remember, when we specify the lifetime parameters in this function signature,
502
+ we’re not changing the lifetimes of any values passed in or returned. Rather,
503
+ we’re specifying that the borrow checker should reject any values that don’t
504
+ adhere to these constraints. Note that the `longest` function doesn’t need to
505
+ know exactly how long `x` and `y` will live, only that some scope can be
506
+ substituted for `'a` that will satisfy this signature.
510
507
-->
511
508
512
509
これで関数シグニチャは、何らかのライフタイム` 'a ` に対して、関数は2つの引数を取り、
@@ -965,7 +962,7 @@ After writing a lot of Rust code, the Rust team found that Rust programmers
965
962
were entering the same lifetime annotations over and over in particular
966
963
situations. These situations were predictable and followed a few deterministic
967
964
patterns. The developers programmed these patterns into the compiler’s code so
968
- the borrow checker could infer the lifetimes in these situations and wouldn' t
965
+ the borrow checker could infer the lifetimes in these situations and wouldn’ t
969
966
need explicit annotations.
970
967
-->
971
968
@@ -1021,7 +1018,8 @@ The compiler uses three rules to figure out what lifetimes references have when
1021
1018
there aren’t explicit annotations. The first rule applies to input lifetimes,
1022
1019
and the second and third rules apply to output lifetimes. If the compiler gets
1023
1020
to the end of the three rules and there are still references for which it can’t
1024
- figure out lifetimes, the compiler will stop with an error.
1021
+ figure out lifetimes, the compiler will stop with an error. These rules apply
1022
+ to `fn` definitions as well as `impl` blocks.
1025
1023
-->
1026
1024
1027
1025
コンパイラは3つの規則を活用し、明示的な注釈がない時に、参照がどんなライフタイムになるかを計算します。
@@ -1215,7 +1213,7 @@ impl<'a> ImportantExcerpt<'a> {
1215
1213
```
1216
1214
1217
1215
<!--
1218
- The lifetime parameter declaration after `impl` and use after the type name
1216
+ The lifetime parameter declaration after `impl` and its use after the type name
1219
1217
are required, but we’re not required to annotate the lifetime of the reference
1220
1218
to `self` because of the first elision rule.
1221
1219
-->
@@ -1262,9 +1260,9 @@ and all lifetimes have been accounted for.
1262
1260
### 静的ライフタイム
1263
1261
1264
1262
<!--
1265
- One special lifetime we need to discuss is `'static`, which denotes the entire
1266
- duration of the program. All string literals have the `'static` lifetime, which
1267
- we can annotate as follows:
1263
+ One special lifetime we need to discuss is `'static`, which means that this
1264
+ reference *can* live for the entire duration of the program. All string
1265
+ literals have the `'static` lifetime, which we can annotate as follows:
1268
1266
-->
1269
1267
1270
1268
議論する必要のある1種の特殊なライフタイムが、` 'static ` であり、これはプログラム全体の期間を示します。
@@ -1276,7 +1274,7 @@ let s: &'static str = "I have a static lifetime.";
1276
1274
```
1277
1275
1278
1276
<!--
1279
- The text of this string is stored directly in the binary of your program, which
1277
+ The text of this string is stored directly in the program’s binary , which
1280
1278
is always available. Therefore, the lifetime of all string literals is
1281
1279
`'static`.
1282
1280
-->
@@ -1374,12 +1372,21 @@ analysis happens at compile time, which doesn’t affect runtime performance!
1374
1372
<!--
1375
1373
Believe it or not, there is much more to learn on the topics we discussed in
1376
1374
this chapter: Chapter 17 discusses trait objects, which are another way to use
1377
- traits. Chapter 19 covers more complex scenarios involving lifetime annotations
1378
- as well as some advanced type system features. But next, you’ll learn how to
1379
- write tests in Rust so you can make sure your code is working the way it should.
1375
+ traits. There are also more complex scenarios involving lifetime annotations
1376
+ that you will only need in very advanced scenarios; for those, you should read
1377
+ the [Rust Reference][reference]. But next, you’ll learn how to write tests in
1378
+ Rust so you can make sure your code is working the way it should.
1380
1379
-->
1381
1380
1382
1381
信じるかどうかは自由ですが、この章で議論した話題にはもっともっと学ぶべきことがあります:
1383
1382
第17章ではトレイトオブジェクトを議論します。これはトレイトを使用する別の手段です。
1384
1383
第19章では、ライフタイム注釈が関わるもっと複雑な筋書きと何か高度な型システムの機能を講義します。
1385
1384
ですが次は、コードがあるべき通りに動いていることを確かめられるように、Rustでテストを書く方法を学びます。
1385
+
1386
+ <!--
1387
+ [references-and-borrowing]:
1388
+ ch04-02-references-and-borrowing.html#references-and-borrowing
1389
+ [string-slices-as-parameters]:
1390
+ ch04-03-slices.html#string-slices-as-parameters
1391
+ [reference]: ../reference/index.html
1392
+ -->
0 commit comments