Skip to content

Commit 8fca5b4

Browse files
committed
Translate a paragraph
1 parent 831c5de commit 8fca5b4

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/subtyping.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ fn main() {
196196
}
197197
```
198198

199+
<!--
199200
The signature of `overwrite` is clearly valid: it takes mutable references to
200201
two values of the same type, and overwrites one with the other. If `&mut T` was
201202
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
204205
`string`, and `overwrite` would be called successfully. `string` would
205206
subsequently be dropped, and `forever_str` would point to freed memory when we
206207
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` は非変性である必要があるのです。
207218

208219
This is the general theme of variance vs invariance: if variance would allow you
209220
to store a short-lived value into a longer-lived slot, then you must be

0 commit comments

Comments
 (0)