File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff line change @@ -196,6 +196,7 @@ fn main() {
196
196
}
197
197
```
198
198
199
+ <!--
199
200
The signature of `overwrite` is clearly valid: it takes mutable references to
200
201
two values of the same type, and overwrites one with the other. If `&mut T` was
201
202
variant over T, then `&mut &'static str` would be a subtype of `&mut &'a str`,
@@ -204,6 +205,16 @@ since `&'static str` is a subtype of `&'a str`. Therefore the lifetime of
204
205
`string`, and `overwrite` would be called successfully. `string` would
205
206
subsequently be dropped, and `forever_str` would point to freed memory when we
206
207
print it! Therefore `&mut` should be invariant.
208
+ -->
209
+
210
+ ` overwrite ` のシグネチャは明らかに文法的に正しいです。これは、同じ型の 2 つの値の
211
+ 可変参照を受け取り、片方の値をもう一つの値で上書きします。
212
+ もし ` &mut T ` が T において変性だったなら、 ` &mut &'static str ` は ` &mut &'a str ` の
213
+ 派生型だったでしょう。 ` &'static str ` が ` &'a str ` の派生型であるからです。
214
+ それ故に、 ` forever_str ` のライフタイムは、見事により短い ` string ` のライフタイムに
215
+ 「縮まる」でしょう。そして、 ` overwrite ` の呼び出しは成功するでしょう。後に ` string ` は
216
+ ドロップされ、 ` forever_str ` は出力の際に解放されたメモリを指していたでしょう!
217
+ それ故に ` &mut ` は非変性である必要があるのです。
207
218
208
219
This is the general theme of variance vs invariance: if variance would allow you
209
220
to store a short-lived value into a longer-lived slot, then you must be
You can’t perform that action at this time.
0 commit comments