@@ -17,24 +17,39 @@ let s: &'static str = "hello world";
17
17
fn generic<T>(x: T) where T: 'static {}
18
18
```
19
19
20
+ <!--
20
21
Both are related but subtly different and this is a common source for
21
22
confusion when learning Rust. Here are some examples for each situation:
23
+ -->
24
+ 2つの状況における` static ` は微妙に異なる意味を持っており、Rustを学ぶときの混乱の元になっています。
25
+ いくつかの例とともにそれぞれの使い方を見てみましょう。
22
26
27
+ <!--
23
28
## Reference lifetime
29
+ -->
30
+ ## 参照のライフタイム
24
31
32
+ <!--
25
33
As a reference lifetime `'static` indicates that the data pointed to by
26
34
the reference lives for the entire lifetime of the running program.
27
35
It can still be coerced to a shorter lifetime.
36
+ -->
37
+ 参照のライフタイムが` 'static ` であることは、参照が指し示す値がプログラムの実行中に渡って生き続けることを示します。
38
+ また、より短いライフタイムに圧縮することも可能です。
28
39
40
+ <!--
29
41
There are two ways to make a variable with `'static` lifetime, and both
30
42
are stored in the read-only memory of the binary:
43
+ -->
44
+ ` 'static ` ライフタイムを持つ変数を作るには下記の2つ方法があります。
45
+ どちらの場合も、値は読み取り専用のメモリ領域に格納されます。
31
46
32
47
<!--
33
48
* Make a constant with the `static` declaration.
34
49
* Make a `string` literal which has type: `&'static str`.
35
50
-->
36
51
* ` static ` 宣言とともに定数を作成する。
37
- * 文字列リテラル で ` &'static str ` 型を持つ変数を作成する。
52
+ * 文字列リテラルで ` &'static str ` 型を持つ変数を作成する。
38
53
39
54
<!--
40
55
See the following example for a display of each method:
@@ -83,15 +98,25 @@ fn main() {
83
98
}
84
99
```
85
100
101
+ <!--
86
102
## Trait bound
103
+ -->
104
+ ## トレイト境界
87
105
106
+ <!--
88
107
As a trait bound, it means the type does not contain any non-static
89
108
references. Eg. the receiver can hold on to the type for as long as
90
109
they want and it will never become invalid until they drop it.
110
+ -->
111
+ トレイト境界としての` 'static ` は型が非静的な参照を含まないことを意味します。
112
+ 言い換えると、レシーバはその型をいくらでも長く保持することができ、意図的にドロップするまでは決して無効になることはないということです。
91
113
114
+ <!--
92
115
It's important to understand this means that any owned data always passes
93
116
a `'static` lifetime bound, but a reference to that owned data generally
94
117
does not:
118
+ -->
119
+ 次のポイントを押さえておきましょう。所有権のある値が` 'static ` ライフタイム境界をパスするとしても、その値への参照が` 'static ` ライフタイム境界をパスするとは限りません。
95
120
96
121
``` rust,editable,compile_fail
97
122
use std::fmt::Debug;
@@ -102,26 +127,35 @@ fn print_it( input: impl Debug + 'static ) {
102
127
103
128
fn main() {
104
129
// i is owned and contains no references, thus it's 'static:
130
+ // i は所有されていて、かつ参照を含まないので 'static
105
131
let i = 5;
106
132
print_it(i);
107
133
108
134
// oops, &i only has the lifetime defined by the scope of
109
135
// main(), so it's not 'static:
136
+ // おっと、&i は main() で定義されたライフタイムしかもたないため 'static ではない
110
137
print_it(&i);
111
138
}
112
139
```
140
+ <!--
113
141
The compiler will tell you:
142
+ -->
143
+ コンパイラのメッセージはこのようになります、
114
144
``` ignore
115
145
error[E0597]: `i` does not live long enough
146
+ エラー[E0597]: `i`は十分なライフタイムを持っていません
116
147
--> src/lib.rs:15:15
117
148
|
118
149
15 | print_it(&i);
119
150
| ---------^^--
120
151
| | |
121
152
| | borrowed value does not live long enough
153
+ | | 借用した値のライフタイムが不足
122
154
| argument requires that `i` is borrowed for `'static`
155
+ | 引数は`i`が`'static`として借用されることを要求する
123
156
16 | }
124
157
| - `i` dropped here while still borrowed
158
+ | `i`は借用されたままここでドロップされる
125
159
```
126
160
127
161
<!--
0 commit comments