Skip to content

Commit 8ed2686

Browse files
authored
Merge pull request #167 from buzztaiki/lifetime
ch10-03, ch19-00 を原文と再同期して「高度なライフタイム」への参照を削除
2 parents 9ab388b + d2d73d2 commit 8ed2686

File tree

2 files changed

+62
-53
lines changed

2 files changed

+62
-53
lines changed

src/ch10-03-lifetime-syntax.md

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
## ライフタイムで参照を検証する
66

77
<!--
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.
1618
-->
1719

18-
第4章の「参照と借用」節で議論しなかった詳細の一つに、Rustにおいて参照は全てライフタイムを保持するということがあります。
20+
21+
第4章の[「参照と借用」][references-and-borrowing]節で議論しなかった詳細の一つに、Rustにおいて参照は全てライフタイムを保持するということがあります。
1922
ライフタイムとは、その参照が有効になるスコープのことです。多くの場合、型が推論されるように、
2023
大体の場合、ライフタイムも暗黙的に推論されます。複数の型の可能性があるときには、型を注釈しなければなりません。
2124
同様に、参照のライフタイムがいくつか異なる方法で関係することがある場合には注釈しなければなりません。
@@ -30,14 +33,12 @@ The concept of lifetimes is somewhat different from tools in other programming
3033
languages, arguably making lifetimes Rust’s most distinctive feature. Although
3134
we won’t cover lifetimes in their entirety in this chapter, we’ll discuss
3235
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.
36+
the concepts.
3537
-->
3638

3739
ライフタイムの概念は、他のプログラミング言語の道具とはどこか異なり、間違いなく、
3840
Rustで一番際立った機能になっています。この章では、ライフタイムの全体を解説することはしませんが、
3941
ライフタイム記法が必要となる最も一般的な場合について議論しますので、ライフタイムの概念について馴染むことができるでしょう。
40-
もっと詳しく知るには、第19章の「高度なライフタイム」節を参照してください。
4142

4243
<!--
4344
### Preventing Dangling References with Lifetimes
@@ -77,9 +78,9 @@ has gone out of scope</span>
7778
<span class="caption">リスト10-17: 値がスコープを抜けてしまった参照を使用しようとする</span>
7879

7980
<!--
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
8182
> 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 Rusts
8384
> having no null values. However, if we try to use a variable before giving it
8485
> a value, we’ll get a compile-time error, which shows that Rust indeed does
8586
> not allow null values.
@@ -146,7 +147,7 @@ Rustで、このコードが動くことを許可していたら、`r`は`x`が
146147
<!--
147148
The Rust compiler has a *borrow checker* that compares scopes to determine
148149
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.
150151
-->
151152

152153
Rustコンパイラには、スコープを比較して全ての借用が有効であるかを決定する*借用チェッカー*があります。
@@ -274,23 +275,16 @@ function to find the longer of two string slices</span>
274275
<!--
275276
Note that we want the function to take string slices, which are references,
276277
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.
280282
-->
281283

282284
関数に取ってほしい引数が文字列スライス、つまり参照であることに注意してください。
283285
何故なら、`longest`関数に引数の所有権を奪ってほしくないからです。
284-
この関数に`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-
292286
リスト10-20で使用している引数が、我々が必要としているものである理由についてもっと詳しい議論は、
293-
第4章の「引数としての文字列スライス」節をご参照ください。
287+
第4章の[「引数としての文字列スライス」][string-slices-as-parameters]節をご参照ください。
294288

295289
<!--
296290
If we try to implement the `longest` function as shown in Listing 10-21, it
@@ -500,19 +494,24 @@ The function signature now tells Rust that for some lifetime `'a`, the function
500494
takes two parameters, both of which are string slices that live at least as
501495
long as lifetime `'a`. The function signature also tells Rust that the string
502496
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.
497+
In practice, it means that the lifetime of the reference returned by the
498+
`longest` function is the same as the smaller of the lifetimes of the
499+
references passed in. These constraints are what we want Rust to enforce.
500+
Remember, when we specify the lifetime parameters in this function signature,
501+
we’re not changing the lifetimes of any values passed in or returned. Rather,
502+
we’re specifying that the borrow checker should reject any values that don’t
503+
adhere to these constraints. Note that the `longest` function doesn’t need to
504+
know exactly how long `x` and `y` will live, only that some scope can be
505+
substituted for `'a` that will satisfy this signature.
510506
-->
511507

512508
これで関数シグニチャは、何らかのライフタイム`'a`に対して、関数は2つの引数を取り、
513509
どちらも少なくともライフタイム`'a`と同じだけ生きる文字列スライスであるとコンパイラに教えるようになりました。
514510
また、この関数シグニチャは、関数から返る文字列スライスも少なくともライフタイム`'a`と同じだけ生きると、
515-
コンパイラに教えています。これらの制約は、まさに私たちがコンパイラに保証してほしかったものです。
511+
コンパイラに教えています。
512+
実際には、`longest`関数が返す参照のライフタイムは、渡された参照のうち、小さい方のライフタイムと同じであるという事です。
513+
これらの制約は、まさに私たちがコンパイラに保証してほしかったものです。
514+
516515
この関数シグニチャでライフタイム引数を指定する時、渡されたり、返したりした、いかなる値のライフタイムも変更していないことを思い出してください。
517516
むしろ、借用チェッカーは、これらの制約を守らない値全てを拒否するべきと指定しています。
518517
`longest`関数は、`x``y`の正確な生存期間を知っている必要はなく、
@@ -965,7 +964,7 @@ After writing a lot of Rust code, the Rust team found that Rust programmers
965964
were entering the same lifetime annotations over and over in particular
966965
situations. These situations were predictable and followed a few deterministic
967966
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
967+
the borrow checker could infer the lifetimes in these situations and wouldnt
969968
need explicit annotations.
970969
-->
971970

@@ -1021,13 +1020,14 @@ The compiler uses three rules to figure out what lifetimes references have when
10211020
there aren’t explicit annotations. The first rule applies to input lifetimes,
10221021
and the second and third rules apply to output lifetimes. If the compiler gets
10231022
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.
1023+
figure out lifetimes, the compiler will stop with an error. These rules apply
1024+
to `fn` definitions as well as `impl` blocks.
10251025
-->
10261026

10271027
コンパイラは3つの規則を活用し、明示的な注釈がない時に、参照がどんなライフタイムになるかを計算します。
10281028
最初の規則は入力ライフタイムに適用され、2番目と3番目の規則は出力ライフタイムに適用されます。
10291029
コンパイラが3つの規則の最後まで到達し、それでもライフタイムを割り出せない参照があったら、
1030-
コンパイラはエラーで停止します。
1030+
コンパイラはエラーで停止します。これらのルールは`fn`の定義にも`impl`ブロックにも適用されます。
10311031

10321032
<!--
10331033
The first rule is that each parameter that is a reference gets its own lifetime
@@ -1215,7 +1215,7 @@ impl<'a> ImportantExcerpt<'a> {
12151215
```
12161216

12171217
<!--
1218-
The lifetime parameter declaration after `impl` and use after the type name
1218+
The lifetime parameter declaration after `impl` and its use after the type name
12191219
are required, but we’re not required to annotate the lifetime of the reference
12201220
to `self` because of the first elision rule.
12211221
-->
@@ -1262,12 +1262,12 @@ and all lifetimes have been accounted for.
12621262
### 静的ライフタイム
12631263

12641264
<!--
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:
1265+
One special lifetime we need to discuss is `'static`, which means that this
1266+
reference *can* live for the entire duration of the program. All string
1267+
literals have the `'static` lifetime, which we can annotate as follows:
12681268
-->
12691269

1270-
議論する必要のある1種の特殊なライフタイムが、`'static`であり、これはプログラム全体の期間を示します
1270+
議論する必要のある1種の特殊なライフタイムが、`'static`であり、これは、この参照がプログラムの全期間生存*できる*事を意味します
12711271
文字列リテラルは全て`'static`ライフタイムになり、次のように注釈できます:
12721272

12731273
```rust
@@ -1276,7 +1276,7 @@ let s: &'static str = "I have a static lifetime.";
12761276
```
12771277

12781278
<!--
1279-
The text of this string is stored directly in the binary of your program, which
1279+
The text of this string is stored directly in the program’s binary, which
12801280
is always available. Therefore, the lifetime of all string literals is
12811281
`'static`.
12821282
-->
@@ -1374,12 +1374,23 @@ analysis happens at compile time, which doesn’t affect runtime performance!
13741374
<!--
13751375
Believe it or not, there is much more to learn on the topics we discussed in
13761376
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.
1377+
traits. There are also more complex scenarios involving lifetime annotations
1378+
that you will only need in very advanced scenarios; for those, you should read
1379+
the [Rust Reference][reference]. But next, you’ll learn how to write tests in
1380+
Rust so you can make sure your code is working the way it should.
13801381
-->
13811382

1383+
1384+
1385+
1386+
13821387
信じるかどうかは自由ですが、この章で議論した話題にはもっともっと学ぶべきことがあります:
13831388
第17章ではトレイトオブジェクトを議論します。これはトレイトを使用する別の手段です。
1384-
第19章では、ライフタイム注釈が関わるもっと複雑な筋書きと何か高度な型システムの機能を講義します。
1389+
非常に高度な筋書きの場合でのみ必要になる、ライフタイム注釈が関わる、もっと複雑な筋書きもあります。 それらについては、[Rust Reference][reference]をお読みください。
13851390
ですが次は、コードがあるべき通りに動いていることを確かめられるように、Rustでテストを書く方法を学びます。
1391+
1392+
[references-and-borrowing]:
1393+
ch04-02-references-and-borrowing.html#参照と借用
1394+
[string-slices-as-parameters]:
1395+
ch04-03-slices.html#引数としての文字列スライス
1396+
[reference]: https://doc.rust-lang.org/reference/

src/ch19-00-advanced-features.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ In this chapter, we’ll cover:
2727

2828
<!--
2929
* Unsafe Rust: how to opt out of some of Rust’s guarantees and take
30-
responsibility for manually upholding those guarantees
31-
* Advanced lifetimes: syntax for complex lifetime situations
30+
responsibility for manually upholding those guarantees
3231
* Advanced traits: associated types, default type parameters, fully qualified
33-
syntax, supertraits, and the newtype pattern in relation to traits
32+
syntax, supertraits, and the newtype pattern in relation to traits
3433
* Advanced types: more about the newtype pattern, type aliases, the never type,
35-
and dynamically sized types
34+
and dynamically sized types
3635
* Advanced functions and closures: function pointers and returning closures
3736
* Macros: ways to define code that defines more code at compile time
3837
-->
3938

4039
* Unsafe Rust: Rustの保証の一部を抜けてその保証を手動で保持する責任を負う方法
41-
* 高度なライフタイム: 複雑なライフタイム状況の記法
4240
* 高度なトレイト: 関連型、デフォルト型引数、フルパス記法、スーパートレイト、トレイトに関連するニュータイプパターン
4341
* 高度な型: ニュータイプパターンについてもっと、型エイリアス、never型、動的サイズ決定型
4442
* 高度な関数とクロージャ: 関数ポインタとクロージャの返却

0 commit comments

Comments
 (0)