Skip to content

Commit 0f1e28d

Browse files
committed
[ch10-03] コメントで残してある原文を最新の master と同期
1 parent 328b2f9 commit 0f1e28d

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

src/ch10-03-lifetime-syntax.md

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
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

1820
第4章の「参照と借用」節で議論しなかった詳細の一つに、Rustにおいて参照は全てライフタイムを保持するということがあります。
@@ -30,8 +32,7 @@ The concept of lifetimes is somewhat different from tools in other programming
3032
languages, arguably making lifetimes Rust’s most distinctive feature. Although
3133
we won’t cover lifetimes in their entirety in this chapter, we’ll discuss
3234
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.
3536
-->
3637

3738
ライフタイムの概念は、他のプログラミング言語の道具とはどこか異なり、間違いなく、
@@ -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,21 +275,15 @@ 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`関数に引数の所有権を奪ってほしくないからです。
284286
この関数に`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-
292287
リスト10-20で使用している引数が、我々が必要としているものである理由についてもっと詳しい議論は、
293288
第4章の「引数としての文字列スライス」節をご参照ください。
294289

@@ -500,13 +495,15 @@ The function signature now tells Rust that for some lifetime `'a`, the function
500495
takes two parameters, both of which are string slices that live at least as
501496
long as lifetime `'a`. The function signature also tells Rust that the string
502497
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.
510507
-->
511508

512509
これで関数シグニチャは、何らかのライフタイム`'a`に対して、関数は2つの引数を取り、
@@ -965,7 +962,7 @@ After writing a lot of Rust code, the Rust team found that Rust programmers
965962
were entering the same lifetime annotations over and over in particular
966963
situations. These situations were predictable and followed a few deterministic
967964
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 wouldnt
969966
need explicit annotations.
970967
-->
971968

@@ -1021,7 +1018,8 @@ The compiler uses three rules to figure out what lifetimes references have when
10211018
there aren’t explicit annotations. The first rule applies to input lifetimes,
10221019
and the second and third rules apply to output lifetimes. If the compiler gets
10231020
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.
10251023
-->
10261024

10271025
コンパイラは3つの規則を活用し、明示的な注釈がない時に、参照がどんなライフタイムになるかを計算します。
@@ -1215,7 +1213,7 @@ impl<'a> ImportantExcerpt<'a> {
12151213
```
12161214

12171215
<!--
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
12191217
are required, but we’re not required to annotate the lifetime of the reference
12201218
to `self` because of the first elision rule.
12211219
-->
@@ -1262,9 +1260,9 @@ and all lifetimes have been accounted for.
12621260
### 静的ライフタイム
12631261

12641262
<!--
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:
12681266
-->
12691267

12701268
議論する必要のある1種の特殊なライフタイムが、`'static`であり、これはプログラム全体の期間を示します。
@@ -1276,7 +1274,7 @@ let s: &'static str = "I have a static lifetime.";
12761274
```
12771275

12781276
<!--
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
12801278
is always available. Therefore, the lifetime of all string literals is
12811279
`'static`.
12821280
-->
@@ -1374,12 +1372,21 @@ analysis happens at compile time, which doesn’t affect runtime performance!
13741372
<!--
13751373
Believe it or not, there is much more to learn on the topics we discussed in
13761374
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.
13801379
-->
13811380

13821381
信じるかどうかは自由ですが、この章で議論した話題にはもっともっと学ぶべきことがあります:
13831382
第17章ではトレイトオブジェクトを議論します。これはトレイトを使用する別の手段です。
13841383
第19章では、ライフタイム注釈が関わるもっと複雑な筋書きと何か高度な型システムの機能を講義します。
13851384
ですが次は、コードがあるべき通りに動いていることを確かめられるように、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

Comments
 (0)