@@ -8149,21 +8149,36 @@ <h1 id="スタティックライフタイム"><a class="header" href="#スタテ
8149
8149
8150
8150
// 'static as part of a trait bound:
8151
8151
fn generic<T>(x: T) where T: 'static {}</code></pre></pre>
8152
- <p>Both are related but subtly different and this is a common source for
8153
- confusion when learning Rust. Here are some examples for each situation:</p>
8154
- <h2 id="reference-lifetime"><a class="header" href="#reference-lifetime">Reference lifetime</a></h2>
8155
- <p>As a reference lifetime <code>'static</code> indicates that the data pointed to by
8152
+ <!--
8153
+ Both are related but subtly different and this is a common source for
8154
+ confusion when learning Rust. Here are some examples for each situation:
8155
+ -->
8156
+ <p>2つの状況における<code>static</code>は微妙に異なる意味を持っており、Rustを学ぶときの混乱の元になっています。
8157
+ いくつかの例とともにそれぞれの使い方を見てみましょう。</p>
8158
+ <!--
8159
+ ## Reference lifetime
8160
+ -->
8161
+ <h2 id="参照のライフタイム"><a class="header" href="#参照のライフタイム">参照のライフタイム</a></h2>
8162
+ <!--
8163
+ As a reference lifetime `'static` indicates that the data pointed to by
8156
8164
the reference lives for the entire lifetime of the running program.
8157
- It can still be coerced to a shorter lifetime.</p>
8158
- <p>There are two ways to make a variable with <code>'static</code> lifetime, and both
8159
- are stored in the read-only memory of the binary:</p>
8165
+ It can still be coerced to a shorter lifetime.
8166
+ -->
8167
+ <p>参照のライフタイムが<code>'static</code>であることは、参照が指し示す値がプログラムの実行中に渡って生き続けることを示します。
8168
+ また、より短いライフタイムに圧縮することも可能です。</p>
8169
+ <!--
8170
+ There are two ways to make a variable with `'static` lifetime, and both
8171
+ are stored in the read-only memory of the binary:
8172
+ -->
8173
+ <p><code>'static</code>ライフタイムを持つ変数を作るには下記の2つ方法があります。
8174
+ どちらの場合も、値は読み取り専用のメモリ領域に格納されます。</p>
8160
8175
<!--
8161
8176
* Make a constant with the `static` declaration.
8162
8177
* Make a `string` literal which has type: `&'static str`.
8163
8178
-->
8164
8179
<ul>
8165
8180
<li><code>static</code>宣言とともに定数を作成する。</li>
8166
- <li>文字列リテラル で <code>&'static str</code>型を持つ変数を作成する。</li>
8181
+ <li>文字列リテラルで <code>&'static str</code>型を持つ変数を作成する。</li>
8167
8182
</ul>
8168
8183
<!--
8169
8184
See the following example for a display of each method:
@@ -8208,13 +8223,23 @@ <h2 id="reference-lifetime"><a class="header" href="#reference-lifetime">Referen
8208
8223
8209
8224
println!("NUM: {} stays accessible!", NUM);
8210
8225
}</code></pre></pre>
8211
- <h2 id="trait-bound"><a class="header" href="#trait-bound">Trait bound</a></h2>
8212
- <p>As a trait bound, it means the type does not contain any non-static
8226
+ <!--
8227
+ ## Trait bound
8228
+ -->
8229
+ <h2 id="トレイト境界"><a class="header" href="#トレイト境界">トレイト境界</a></h2>
8230
+ <!--
8231
+ As a trait bound, it means the type does not contain any non-static
8213
8232
references. Eg. the receiver can hold on to the type for as long as
8214
- they want and it will never become invalid until they drop it.</p>
8215
- <p>It's important to understand this means that any owned data always passes
8216
- a <code>'static</code> lifetime bound, but a reference to that owned data generally
8217
- does not:</p>
8233
+ they want and it will never become invalid until they drop it.
8234
+ -->
8235
+ <p>トレイト境界としての<code>'static</code>は型が非静的な参照を含まないことを意味します。
8236
+ 言い換えると、レシーバはその型をいくらでも長く保持することができ、意図的にドロップするまでは決して無効になることはないということです。</p>
8237
+ <!--
8238
+ It's important to understand this means that any owned data always passes
8239
+ a `'static` lifetime bound, but a reference to that owned data generally
8240
+ does not:
8241
+ -->
8242
+ <p>次のポイントを押さえておきましょう。所有権のある値が<code>'static</code>ライフタイム境界をパスするとしても、その値への参照が<code>'static</code>ライフタイム境界をパスするとは限りません。</p>
8218
8243
<pre><pre class="playground"><code class="language-rust editable compile_fail edition2021">use std::fmt::Debug;
8219
8244
8220
8245
fn print_it( input: impl Debug + 'static ) {
@@ -8223,24 +8248,33 @@ <h2 id="trait-bound"><a class="header" href="#trait-bound">Trait bound</a></h2>
8223
8248
8224
8249
fn main() {
8225
8250
// i is owned and contains no references, thus it's 'static:
8251
+ // i は所有されていて、かつ参照を含まないので 'static
8226
8252
let i = 5;
8227
8253
print_it(i);
8228
8254
8229
8255
// oops, &i only has the lifetime defined by the scope of
8230
8256
// main(), so it's not 'static:
8257
+ // おっと、&i は main() で定義されたライフタイムしかもたないため 'static ではない
8231
8258
print_it(&i);
8232
8259
}</code></pre></pre>
8233
- <p>The compiler will tell you:</p>
8260
+ <!--
8261
+ The compiler will tell you:
8262
+ -->
8263
+ <p>コンパイラのメッセージはこのようになります、</p>
8234
8264
<pre><code class="language-ignore">error[E0597]: `i` does not live long enough
8265
+ エラー[E0597]: `i`は十分なライフタイムを持っていません
8235
8266
--> src/lib.rs:15:15
8236
8267
|
8237
8268
15 | print_it(&i);
8238
8269
| ---------^^--
8239
8270
| | |
8240
8271
| | borrowed value does not live long enough
8272
+ | | 借用した値のライフタイムが不足
8241
8273
| argument requires that `i` is borrowed for `'static`
8274
+ | 引数は`i`が`'static`として借用されることを要求する
8242
8275
16 | }
8243
8276
| - `i` dropped here while still borrowed
8277
+ | `i`は借用されたままここでドロップされる
8244
8278
</code></pre>
8245
8279
<!--
8246
8280
### See also:
0 commit comments